Files
SWCompression/docs/index.html
T
Timofey Solomko b471e66106 Prepare for 4.0.1
2017-11-25 18:43:39 +03:00

511 lines
24 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html lang="en">
<head>
<title>SWCompression Reference</title>
<link rel="stylesheet" type="text/css" href="css/jazzy.css" />
<link rel="stylesheet" type="text/css" href="css/highlight.css" />
<meta charset="utf-8">
<script src="js/jquery.min.js" defer></script>
<script src="js/jazzy.js" defer></script>
<script src="js/lunr.min.js" defer></script>
<script src="js/typeahead.jquery.js" defer></script>
<script src="js/jazzy.search.js" defer></script>
</head>
<body>
<a title="SWCompression Reference"></a>
<header class="header">
<p class="header-col header-col--primary">
<a class="header-link" href="index.html">
SWCompression Docs
</a>
(100% documented)
</p>
<p class="header-col--secondary">
<form role="search" action="search.json">
<input type="text" placeholder="Search documentation" data-typeahead>
</form>
</p>
<p class="header-col header-col--secondary">
<a class="header-link" href="https://github.com/tsolomko/SWCompression">
<img class="header-icon" src="img/gh.png"/>
View on GitHub
</a>
</p>
</header>
<p class="breadcrumbs">
<a class="breadcrumb" href="index.html">SWCompression Reference</a>
<img class="carat" src="img/carat.png" />
SWCompression Reference
</p>
<div class="content-wrapper">
<nav class="navigation">
<ul class="nav-groups">
<li class="nav-group-name">
<a class="nav-group-name-link" href="Compression.html">Compression</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a class="nav-group-task-link" href="Classes/BZip2.html">BZip2</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Classes/BZip2/BlockSize.html"> BlockSize</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Classes/Deflate.html">Deflate</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Classes/LZMA.html">LZMA</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Classes/LZMA2.html">LZMA2</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a class="nav-group-name-link" href="Archives.html">Archives</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a class="nav-group-task-link" href="Classes/GzipArchive.html">GzipArchive</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Classes/GzipArchive/Member.html"> Member</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Structs/GzipHeader.html">GzipHeader</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Classes/XZArchive.html">XZArchive</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Classes/ZlibArchive.html">ZlibArchive</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Structs/ZlibHeader.html">ZlibHeader</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Structs/ZlibHeader/CompressionLevel.html"> CompressionLevel</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a class="nav-group-name-link" href="Containers.html">Containers</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a class="nav-group-task-link" href="Classes/SevenZipContainer.html">SevenZipContainer</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Structs/SevenZipEntry.html">SevenZipEntry</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Structs/SevenZipEntryInfo.html">SevenZipEntryInfo</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Classes/TarContainer.html">TarContainer</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Structs/TarEntry.html">TarEntry</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Structs/TarEntryInfo.html">TarEntryInfo</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Classes/ZipContainer.html">ZipContainer</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Structs/ZipEntry.html">ZipEntry</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Structs/ZipEntryInfo.html">ZipEntryInfo</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a class="nav-group-name-link" href="Errors.html">Errors</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/BZip2Error.html">BZip2Error</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/DeflateError.html">DeflateError</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/GzipError.html">GzipError</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/LZMAError.html">LZMAError</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/LZMA2Error.html">LZMA2Error</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/SevenZipError.html">SevenZipError</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/TarError.html">TarError</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/XZError.html">XZError</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/ZipError.html">ZipError</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/ZlibError.html">ZlibError</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a class="nav-group-name-link" href="Protocols.html">Protocols</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a class="nav-group-task-link" href="Protocols/Archive.html">Archive</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Protocols/Container.html">Container</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Protocols/ContainerEntry.html">ContainerEntry</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Protocols/ContainerEntryInfo.html">ContainerEntryInfo</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Protocols/CompressionAlgorithm.html">CompressionAlgorithm</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Protocols/DecompressionAlgorithm.html">DecompressionAlgorithm</a>
</li>
</ul>
</li>
<li class="nav-group-name">
<a class="nav-group-name-link" href="Common Auxiliary Types.html">Common Auxiliary Types</a>
<ul class="nav-group-tasks">
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/ContainerEntryType.html">ContainerEntryType</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Structs/DosAttributes.html">DosAttributes</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Structs/Permissions.html">Permissions</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/CompressionMethod.html">CompressionMethod</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/FileSystemType.html">FileSystemType</a>
</li>
</ul>
</li>
</ul>
</nav>
<article class="main-content">
<section class="section">
<div class="section-content">
<h1 id='swcompression' class='heading'>SWCompression</h1>
<p><a href="https://developer.apple.com/swift/"><img src="https://img.shields.io/badge/Swift-4.0-blue.svg" alt="Swift 4"></a>
<a href="https://raw.githubusercontent.com/tsolomko/SWCompression/master/LICENSE"><img src="https://img.shields.io/badge/license-MIT-lightgrey.svg" alt="GitHub license"></a>
<a href="https://travis-ci.org/tsolomko/SWCompression"><img src="https://travis-ci.org/tsolomko/SWCompression.svg?branch=develop" alt="Build Status"></a></p>
<p>A framework with (de)compression algorithms and functions for processing various archives and containers.</p>
<h2 id='what-is-this' class='heading'>What is this?</h2>
<p>SWCompression &mdash; is a framework with a collection of functions for:</p>
<ol>
<li>Decompression (and sometimes compression) using different algorithms.</li>
<li>Reading (and sometimes writing) archives of different formats.</li>
<li>Reading containers such as ZIP, TAR and 7-Zip.</li>
</ol>
<p>It also works both on Apple platforms and <strong>Linux</strong>.</p>
<p>All features are listed in the tables below.
<q>TBD</q> means that feature is planned but not implemented (yet).</p>
<table><thead>
<tr>
<th></th>
<th>Deflate</th>
<th>BZip2</th>
<th>LZMA/LZMA2</th>
</tr>
</thead><tbody>
<tr>
<td>Decompression</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Compression</td>
<td></td>
<td></td>
<td>TBD</td>
</tr>
</tbody></table>
<table><thead>
<tr>
<th></th>
<th>Zlib</th>
<th>GZip</th>
<th>XZ</th>
</tr>
</thead><tbody>
<tr>
<td>Read</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Write</td>
<td></td>
<td></td>
<td>TBD</td>
</tr>
</tbody></table>
<table><thead>
<tr>
<th></th>
<th>ZIP</th>
<th>TAR</th>
<th>7-Zip</th>
</tr>
</thead><tbody>
<tr>
<td>Read</td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Write</td>
<td>TBD</td>
<td>TBD</td>
<td>TBD</td>
</tr>
</tbody></table>
<p>Also, SWCompression is <em>written with Swift only.</em></p>
<h2 id='installation' class='heading'>Installation</h2>
<p>SWCompression can be integrated into your project using Swift Package Manager, CocoaPods or Carthage.</p>
<h3 id='swift-package-manager' class='heading'>Swift Package Manager</h3>
<p>Add SWCompression to you package dependencies and also specify it as a dependency for your target, e.g.:</p>
<pre class="highlight swift"><code><span class="kd">import</span> <span class="kt">PackageDescription</span>
<span class="k">let</span> <span class="nv">package</span> <span class="o">=</span> <span class="kt">Package</span><span class="p">(</span>
<span class="nv">name</span><span class="p">:</span> <span class="s">"PackageName"</span><span class="p">,</span>
<span class="nv">dependencies</span><span class="p">:</span> <span class="p">[</span>
<span class="o">.</span><span class="nf">package</span><span class="p">(</span><span class="nv">url</span><span class="p">:</span> <span class="s">"https://github.com/tsolomko/SWCompression.git"</span><span class="p">,</span>
<span class="nv">from</span><span class="p">:</span> <span class="s">"4.0.0"</span><span class="p">)</span>
<span class="p">],</span>
<span class="nv">targets</span><span class="p">:</span> <span class="p">[</span>
<span class="o">.</span><span class="nf">target</span><span class="p">(</span>
<span class="nv">name</span><span class="p">:</span> <span class="s">"TargetName"</span><span class="p">,</span>
<span class="nv">dependencies</span><span class="p">:</span> <span class="p">[</span><span class="s">"SWCompression"</span><span class="p">]</span>
<span class="p">)</span>
<span class="p">]</span>
<span class="p">)</span>
</code></pre>
<p>More details you can find in <a href="https://github.com/apple/swift-package-manager/tree/master/Documentation">Swift Package Manager&rsquo;s Documentation</a>.</p>
<h3 id='cocoapods' class='heading'>CocoaPods</h3>
<p>Add to your Podfile <code>pod &#39;SWCompression&#39;</code>.</p>
<p>If you need only some parts of framework, you can install only them using sub-podspecs.
Available subspecs:</p>
<ul>
<li>SWCompression/BZip2</li>
<li>SWCompression/Deflate</li>
<li>SWCompression/Gzip</li>
<li>SWCompression/LZMA</li>
<li>SWCompression/LZMA2</li>
<li>SWCompression/SevenZip</li>
<li>SWCompression/TAR</li>
<li>SWCompression/XZ</li>
<li>SWCompression/Zlib</li>
<li>SWCompression/ZIP</li>
</ul>
<p>Also, do not forget to include <code>use_frameworks!</code> line in your Podfile.</p>
<p>To complete installation, run <code>pod install</code>.</p>
<h4 id='q-optional-dependencies-q' class='heading'><q>Optional Dependencies</q></h4>
<p>Both ZIP and 7-Zip containers have a single compression method which is most likely to be used,
for compression of data inside them. This is Deflate for ZIP and LZMA/LZMA2 for 7-Zip.
Thus, SWCompression/ZIP subspec have SWCompression/Deflate subspec as a dependency
and SWCompression/LZMA subspec as a dependency for SWCompression/SevenZip.</p>
<p>But both of these formats support other compression methods, and some of them are implemented in SWCompression.
For CocoaPods configurations there are some sort of &lsquo;optional dependencies&rsquo; for such compression methods.</p>
<p><q>Optional dependency</q> in this context means
that SWCompression/ZIP or SWCompression/7-Zip will support particular compression methods
only if a corresponding subspec is expicitly specified in your Podfile and installed.</p>
<p>List of <q>optional dependecies</q>:</p>
<ul>
<li>For SWCompression/ZIP:
<ul>
<li>SWCompression/BZip2</li>
<li>SWCompression/LZMA</li>
</ul></li>
<li>For SWCompression/SevenZip:
<ul>
<li>SWCompression/BZip2</li>
<li>SWCompression/Deflate</li>
</ul></li>
</ul>
<p><strong>Note:</strong> If you use Carthage or Swift Package Manager you always have the full package
and ZIP and 7-Zip are built with Deflate, BZip2 and LZMA/LZMA2 support.</p>
<h3 id='carthage' class='heading'>Carthage</h3>
<p>Add to your Cartfile <code>github &quot;tsolomko/SWCompression&quot;</code>.</p>
<p>Then run <code>carthage update</code>.</p>
<p>Finally, drag and drop <code>SWCompression.framework</code> from <code>Carthage/Build</code> folder
into the <q>Embedded Binaries</q> section on your targets&rsquo; <q>General</q> tab in Xcode.</p>
<h2 id='usage' class='heading'>Usage</h2>
<h3 id='basic-example' class='heading'>Basic Example</h3>
<p>If you&rsquo;d like to decompress <q>deflated</q> data just use:</p>
<pre class="highlight swift"><code><span class="c1">// let data = &lt;Your compressed data&gt;</span>
<span class="k">let</span> <span class="nv">decompressedData</span> <span class="o">=</span> <span class="k">try</span><span class="p">?</span> <span class="kt">Deflate</span><span class="o">.</span><span class="nf">decompress</span><span class="p">(</span><span class="nv">data</span><span class="p">:</span> <span class="n">data</span><span class="p">)</span>
</code></pre>
<p>However, it is unlikely that you will encounter deflated data outside of any archive.
So, in case of GZip archive you should use:</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">decompressedData</span> <span class="o">=</span> <span class="k">try</span><span class="p">?</span> <span class="kt">GzipArchive</span><span class="o">.</span><span class="nf">unarchive</span><span class="p">(</span><span class="nv">archiveData</span><span class="p">:</span> <span class="n">data</span><span class="p">)</span>
</code></pre>
<h3 id='handling-errors' class='heading'>Handling Errors</h3>
<p>Most SWCompression functions can throw an error and you are responsible for handling them.
If you look at list of available error types and their cases, you may be frightened by their number.
However, most of these cases (such as <code><a href="Enums/XZError.html#/s:13SWCompression7XZErrorO10wrongMagicA2CmF">XZError.wrongMagic</a></code>) exist for diagnostic purposes.</p>
<p>Thus, you only need to handle the most common type of error for your archive/algorithm. For example:</p>
<pre class="highlight swift"><code><span class="k">do</span> <span class="p">{</span>
<span class="c1">// let data = &lt;Your compressed data&gt;</span>
<span class="k">let</span> <span class="nv">decompressedData</span> <span class="o">=</span> <span class="k">try</span> <span class="kt">XZArchive</span><span class="o">.</span><span class="nf">unarchive</span><span class="p">(</span><span class="nv">archive</span><span class="p">:</span> <span class="n">data</span><span class="p">)</span>
<span class="p">}</span> <span class="k">catch</span> <span class="k">let</span> <span class="nv">error</span> <span class="k">as</span> <span class="kt">XZError</span> <span class="p">{</span>
<span class="o">&lt;</span><span class="n">handle</span> <span class="kt">XZ</span> <span class="n">related</span> <span class="n">error</span> <span class="n">here</span><span class="o">&gt;</span>
<span class="p">}</span> <span class="k">catch</span> <span class="k">let</span> <span class="nv">error</span> <span class="p">{</span>
<span class="o">&lt;</span><span class="n">handle</span> <span class="n">all</span> <span class="n">other</span> <span class="n">errors</span> <span class="n">here</span><span class="o">&gt;</span>
<span class="p">}</span>
</code></pre>
<p>Or, if you don&rsquo;t care about errors at all, use <code>try?</code>.</p>
<h3 id='documentation' class='heading'>Documentation</h3>
<p>Every function or type of SWCompression&rsquo;s public API is documented.
This documentation can be found at its own <a href="http://tsolomko.github.io/SWCompression">website</a>.</p>
<h3 id='sophisticated-example' class='heading'>Sophisticated example</h3>
<p>There is a small command-line program, <q>swcomp</q>, which is included in this repository in <q>Sources/swcomp</q>.
To build it you need to uncomment several lines in <q>Package.swift</q> and run <code>swift build -c release</code>.</p>
<h2 id='contributing' class='heading'>Contributing</h2>
<p>Whether you find a bug, have a suggestion, idea or something else,
please <a href="https://github.com/tsolomko/SWCompression/issues">create an issue</a> on GitHub.</p>
<p>In case you have encoutered a bug, it would be especially helpful if you attach a file (archive, etc.)
that caused the bug to happen.</p>
<p>If you&rsquo;d like to contribute code, please <a href="https://github.com/tsolomko/SWCompression/pulls">create a pull request</a> on GitHub.</p>
<h3 id='executing-tests-locally' class='heading'>Executing tests locally</h3>
<p>If you&rsquo;d like to run tests on your computer, you need to do some additional steps after cloning this repository:</p>
<pre class="highlight shell"><code>git submodule update --init --recursive
<span class="nb">cd </span>Tests/Test<span class="se">\ </span>Files
git lfs pull
</code></pre>
<p>These commands fetch example archives and other files which are used for testing.
These files are stored in a <a href="https://github.com/tsolomko/SWCompression-Test-Files">separate repository</a>.
Git LFS is used for storing them which is the reason for having them in the separate repository,
since Swift Package Manager have some problems with Git LFS-enabled repositories
(it requires installing git-lfs <em>locally</em> with <code>--skip-smudge</code> option to solve these problems).</p>
<h2 id='performace' class='heading'>Performace</h2>
<p>Usage of whole module optimizations is recommended for best performance.
These optimizations are enabled by default for Release configurations.</p>
<p><a href="Tests/Results.md">Tests Results</a> document contains results of performance testing of various functions.</p>
<h2 id='why' class='heading'>Why?</h2>
<p>First of all, existing solutions for work with compression, archives and containers have some problems.
They might not support some particular compression algorithms or archive formats and they all have different APIs,
which sometimes can be slightly <q>unfriendly</q> to users.
This project attempts to provide missing (and sometimes existing) functionality through unified API,
which is easy to use and remember.</p>
<p>Secondly, it may be important to have a compression framework written completely in Swift,
without relying on either system libraries or solutions implemented in different languages.
Additionaly, since SWCompression is written fully in Swift without Objective-C,
it can also be compiled on <strong>Linux</strong>.</p>
<h2 id='future-plans' class='heading'>Future plans</h2>
<ul>
<li>Performance&hellip;</li>
<li>Better Deflate compression.</li>
<li>Something else&hellip;</li>
</ul>
<h2 id='references' class='heading'>References</h2>
<ul>
<li><a href="http://www.paul.sladen.org/projects/pyflate/">pyflate</a></li>
<li><a href="https://www.ietf.org/rfc/rfc1951.txt">Deflate specification</a></li>
<li><a href="https://www.ietf.org/rfc/rfc1952.txt">GZip specification</a></li>
<li><a href="https://www.ietf.org/rfc/rfc1950.txt">Zlib specfication</a></li>
<li><a href="http://www.7-zip.org/sdk.html">LZMA SDK and specification</a></li>
<li><a href="http://tukaani.org/xz/xz-file-format-1.0.4.txt">XZ specification</a></li>
<li><a href="https://en.wikipedia.org/wiki/Lempel%E2%80%93Ziv%E2%80%93Markov_chain_algorithm">Wikipedia article about LZMA</a></li>
<li><a href="http://www.pkware.com/appnote">.ZIP Application Note</a></li>
<li><a href="http://www.iso.org/iso/catalogue_detail.htm?csnumber=60101">ISO/IEC 21320-1</a></li>
<li><a href="https://opensource.apple.com/source/zip/zip-6/unzip/unzip/proginfo/extra.fld">List of defined ZIP extra fields</a></li>
<li><a href="https://en.wikipedia.org/wiki/Tar_(computing)">Wikipedia article about TAR</a></li>
<li><a href="http://pubs.opengroup.org/onlinepubs/9699919799/utilities/pax.html">Pax specification</a></li>
<li><a href="https://www.gnu.org/software/tar/manual/html_node/Standard.html">Basic TAR specification</a></li>
<li><a href="https://commons.apache.org/proper/commons-compress/">Apache Commons Compress</a></li>
<li><a href="http://zork.net/%7Est/jottings/sais.html">A walk through the SA-IS Suffix Array Construction Algorithm</a></li>
<li><a href="https://en.wikipedia.org/wiki/Bzip2">Wikipedia article about BZip2</a></li>
</ul>
</div>
</section>
</article>
</div>
<section class="footer">
<p>© 2017 Timofey Solomko</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.9.0</a>, a <a class="link" href="http://realm.io" target="_blank" rel="external">Realm</a> project.</p>
</section>
</body>
</div>
</html>