Files

493 lines
25 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 4.9.1 Docs
</a>
(100% documented)
</p>
<div class="header-col--secondary">
<form role="search" action="search.json">
<input type="text" placeholder="Search documentation" data-typeahead>
</form>
</div>
<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" alt="GitHub"/>
View on GitHub
</a>
</p>
</header>
<p class="breadcrumbs">
<a class="breadcrumb" href="index.html">SWCompression</a>
</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="Structs/LZMAProperties.html">LZMAProperties</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Classes/LZMA2.html">LZMA2</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/LZ4.html">LZ4</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="Structs/GzipHeader/ExtraField.html"> ExtraField</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="7-Zip.html">7-Zip</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>
</ul>
</li>
<li class="nav-group-name">
<a class="nav-group-name-link" href="TAR.html">TAR</a>
<ul class="nav-group-tasks">
<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="Classes/TarContainer/Format.html"> Format</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Structs/TarReader.html">TarReader</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Structs/TarWriter.html">TarWriter</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>
</ul>
</li>
<li class="nav-group-name">
<a class="nav-group-name-link" href="ZIP.html">ZIP</a>
<ul class="nav-group-tasks">
<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>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Protocols/ZipExtraField.html">ZipExtraField</a>
</li>
<li class="nav-group-task">
<a class="nav-group-task-link" href="Enums/ZipExtraFieldLocation.html">ZipExtraFieldLocation</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/DataError.html">DataError</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/TarCreateError.html">TarCreateError</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%20Auxiliary%20Types.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 top-matter">
<h1 id='swcompression' class='heading'>SWCompression</h1>
<p><a href="https://developer.apple.com/swift/"><img src="https://img.shields.io/badge/Swift-5.9+-blue.svg" alt="Swift 5.9+"></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://dev.azure.com/tsolomko/SWCompression/_build/latest?definitionId=3&branchName=develop"><img src="https://dev.azure.com/tsolomko/SWCompression/_apis/build/status/tsolomko.SWCompression?branchName=develop" alt="Build Status"></a></p>
<p>A framework with (de)compression algorithms and functions for working with various archives and containers.</p>
<h2 id='what-is-this' class='heading'>What is this?</h2>
<p>SWCompression 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 (and sometimes writing) containers such as ZIP, TAR and 7-Zip.</li>
</ol>
<p>It also works on Apple platforms, Linux, <strong>and Windows</strong>.</p>
<p>All features are listed in the tables below. &ldquo;TBD&rdquo; 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>
<th>LZ4</th>
</tr>
</thead><tbody>
<tr>
<td>Decompression</td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Compression</td>
<td></td>
<td></td>
<td>TBD</td>
<td></td>
</tr>
</tbody></table>
<table><thead>
<tr>
<th></th>
<th>Zlib</th>
<th>GZip</th>
<th>XZ</th>
<th>ZIP</th>
<th>TAR</th>
<th>7-Zip</th>
</tr>
</thead><tbody>
<tr>
<td>Read</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>Write</td>
<td></td>
<td></td>
<td>TBD</td>
<td>TBD</td>
<td></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.</p>
<p><strong>Note:</strong> SWCompression versions 4.8.6 and earlier were also made available via CocoaPods or Carthage.</p>
<p>To install with Swift Package manager, add SWCompression to you package dependencies and 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">name</span><span class="p">:</span> <span class="s">"SWCompression"</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.9.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/main/Documentation">Swift Package Manager&rsquo;s Documentation</a>.</p>
<h2 id='usage' class='heading'>Usage</h2>
<h3 id='basic-example' class='heading'>Basic Example</h3>
<p>For example, if you want to decompress &ldquo;deflated&rdquo; 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 the 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">archive</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 errors and you are responsible for handling them. If you look at the list of
available error types and their cases, you may be frightened by their number. However, most of the cases (such as
<code><a href="Enums/XZError.html#/s:13SWCompression7XZErrorO10wrongMagicyA2CmF">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="c1">// &lt;handle XZ related error here&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="c1">// &lt;handle all other errors here&gt;</span>
<span class="p">}</span>
</code></pre>
<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> or via a slightly shorter link:
<a href="http://swcompression.tsolomko.me">swcompression.tsolomko.me</a></p>
<h3 id='sophisticated-example' class='heading'>Sophisticated example</h3>
<p>There is a small command-line program, &ldquo;swcomp&rdquo;, which is included in this repository in &ldquo;Sources/swcomp&rdquo;. It can be
built using Swift Package Manager (only available on macOS).</p>
<p><strong>IMPORTANT:</strong> The &ldquo;swcomp&rdquo; command-line tool is NOT intended for general use.</p>
<h2 id='contributing' class='heading'>Contributing</h2>
<p>Whether you find a bug, have a suggestion, idea, feedback or something else, please
<a href="https://github.com/tsolomko/SWCompression/issues">create an issue</a> on GitHub. If you have any questions, you can ask
them on the <a href="https://github.com/tsolomko/SWCompression/discussions">Discussions</a> page.</p>
<p>In the case of a bug, it will be especially helpful if you attach a file (archive, etc.) that caused the bug to occur.</p>
<p>If you&rsquo;d like to contribute, 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 want to run tests on your computer, you need to do a couple of additional steps after cloning the repository:</p>
<pre class="highlight shell"><code>git submodule update <span class="nt">--init</span> <span class="nt">--recursive</span>
<span class="nb">cd</span> <span class="s2">"Tests/Test Files"</span>
<span class="nb">cp </span>gitattributes-copy .gitattributes
git lfs pull
git lfs checkout
</code></pre>
<p>These commands will download the files used in tests which are stored in a
<a href="https://github.com/tsolomko/SWCompression-Test-Files">separate repository</a> using Git LFS. There are two reasons for
this complicated setup. Firstly, some of these files can be quite big, and it would be unfortunate if the users of
SWCompression had to download them during the installation. Secondly, Swift Package Manager and contemporary versions of
Xcode don&rsquo;t always work well with git-lfs-enabled repositories. To prevent any potential problems test files were moved
into another repository.</p>
<p>Please note, that if you want to add a new <em>type</em> of test files, in addition to running <code>git lfs track</code>, you have to
also copy into the &ldquo;Tests/Test Files/gitattributes-copy&rdquo; file a line this command adds to the &ldquo;Tests/Test Files/.gitattributes&rdquo;
file. <strong>Do not commit the &ldquo;.gitattributes&rdquo; file to the git history. It is git-ignored for a reason!</strong></p>
<p>Please also be mindful of Git LFS bandwidth quota on GitHub: try to limit downloading lfs&rsquo;d files using <code>git lfs pull</code>.
In CI we use some caching techniques to help with the quota, so if you&rsquo;re going to add new tests that require several
new test files you should try to submit them all together to reduce the amount of times CI needs to recreate the cache
(recreating the cache requires to do <code>git lfs pull</code> for all test files).</p>
<h2 id='performance' class='heading'>Performance</h2>
<p>Using whole module optimizations is recommended for the best performance. They are enabled by default in the Release build
configuration.</p>
<p><a href="Tests/Results.md">Tests Results</a> document contains results of benchmarking of various functions.</p>
<h2 id='why' class='heading'>Why?</h2>
<p>First of all, existing solutions for working with compression, archives and containers have certain disadvantages. They
might not support a particular compression algorithm or archive format and they all have different APIs, which sometimes
can be slightly confusing for users, especially when you mix different libraries in one project. This project attempts to
provide missing (and sometimes existing) functionality through the unified API which is easy to use and remember.</p>
<p>Secondly, in some cases it may be important to have a compression framework written entirely in Swift, without relying
on either system libraries or solutions implemented in other languages. Additionaly, since SWCompression is written
completely in Swift without Objective-C, it can also be used on Linux, <strong>and Windows</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='license' class='heading'>License</h2>
<p><a href="LICENSE">MIT licensed</a></p>
<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 specification</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://www.systutorials.com/docs/linux/man/5-star/">star man pages</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>
<li><a href="https://github.com/lz4/lz4/blob/dev/doc/lz4_Frame_format.md">LZ4 Frame Format Description</a></li>
<li><a href="https://github.com/lz4/lz4/blob/dev/doc/lz4_Block_format.md">LZ4 Block Format Description</a></li>
<li><a href="https://github.com/Cyan4973/xxHash/blob/dev/doc/xxhash_spec.md">xxHash specification</a></li>
</ul>
</div>
</section>
</article>
</div>
<section class="footer">
<p>© 2026 Timofey Solomko</p>
<p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external noopener">jazzy ♪♫ v0.15.4</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external noopener">Realm</a> project.</p>
</section>
</body>
</html>