Swiftpack.co - Package - tsolomko/SWCompression

SWCompression

Swift 4.1 GitHub license Build Status

A framework with (de)compression algorithms and functions for processing various archives and containers.

What is this?

SWCompression — is a framework with a collection of functions for:

  1. Decompression (and sometimes compression) using different algorithms.
  2. Reading (and sometimes writing) archives of different formats.
  3. Reading containers such as ZIP, TAR and 7-Zip.

It also works both on Apple platforms and Linux.

All features are listed in the tables below. "TBD" means that feature is planned but not implemented (yet).

| | Deflate | BZip2 | LZMA/LZMA2 | | ------------- | ------- | ----- | ---------- | | Decompression | ✅ | ✅ | ✅ | | Compression | ✅ | ✅ | TBD |

| | Zlib | GZip | XZ | | ----- | ---- | ---- | --- | | Read | ✅ | ✅ | ✅ | | Write | ✅ | ✅ | TBD |

| | ZIP | TAR | 7-Zip | | ----- | --- | --- | ----- | | Read | ✅ | ✅ | ✅ | | Write | TBD | TBD | TBD |

Also, SWCompression is written with Swift only.

Installation

SWCompression can be integrated into your project using Swift Package Manager, CocoaPods or Carthage.

Swift Package Manager

Add SWCompression to you package dependencies and specify it as a dependency for your target, e.g.:

import PackageDescription

let package = Package(
    name: "PackageName",
    dependencies: [
        .package(url: "https://github.com/tsolomko/SWCompression.git",
                 from: "4.3.0")
    ],
    targets: [
        .target(
            name: "TargetName",
            dependencies: ["SWCompression"]
        )
    ]
)

More details you can find in Swift Package Manager's Documentation.

CocoaPods

Add pod 'SWCompression', '~> 4.3' and use_frameworks! to your Podfile.

To complete installation, run pod install.

If you need only some parts of framework, you can install only them using sub-podspecs. Available subspecs:

  • SWCompression/BZip2
  • SWCompression/Deflate
  • SWCompression/Gzip
  • SWCompression/LZMA
  • SWCompression/LZMA2
  • SWCompression/SevenZip
  • SWCompression/TAR
  • SWCompression/XZ
  • SWCompression/Zlib
  • SWCompression/ZIP

"Optional Dependencies"

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 is a dependency for SWCompression/SevenZip.

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 'optional dependencies' for such compression methods.

"Optional dependency" 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.

List of "optional dependecies":

  • For SWCompression/ZIP:
    • SWCompression/BZip2
    • SWCompression/LZMA
  • For SWCompression/SevenZip:
    • SWCompression/BZip2
    • SWCompression/Deflate

Note: If you use Swift Package Manager or Carthage you always have everything (ZIP and 7-Zip are built with Deflate, BZip2 and LZMA/LZMA2 support).

Carthage

Add to your Cartfile github "tsolomko/SWCompression" ~> 4.3.

Then run carthage update.

Finally, drag and drop SWCompression.framework from Carthage/Build folder into the "Embedded Binaries" section on your targets' "General" tab in Xcode.

SWCompression depends on BitByteData framework, so Carthage will also download this dependency, and you should drag and drop BitByteData.framework file into the "Embedded Binaries" as well.

Usage

Basic Example

If you'd like to decompress "deflated" data just use:

// let data = <Your compressed data>
let decompressedData = try? Deflate.decompress(data: data)

However, it is unlikely that you will encounter deflated data outside of any archive. So, in case of GZip archive you should use:

let decompressedData = try? GzipArchive.unarchive(archiveData: data)

Handling Errors

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 XZError.wrongMagic) exist for diagnostic purposes.

Thus, you only need to handle the most common type of error for your archive/algorithm. For example:

do {
    // let data = <Your compressed data>
    let decompressedData = try XZArchive.unarchive(archive: data)
} catch let error as XZError {
    <handle XZ related error here>
} catch let error {
    <handle all other errors here>
}

Or, if you don't care about errors at all, use try?.

Documentation

Every function or type of SWCompression's public API is documented. This documentation can be found at its own website.

Sophisticated example

There is a small command-line program, "swcomp", which is included in this repository in "Sources/swcomp". To build it you need to uncomment several lines in "Package.swift" and run swift build -c release.

Contributing

Whether you find a bug, have a suggestion, idea or something else, please create an issue on GitHub.

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.

If you'd like to contribute code, please create a pull request on GitHub.

Note: If you are considering working on SWCompression, please note that Xcode project (SWCompression.xcodeproj) was created manually and you shouldn't use swift package generate-xcodeproj command.

Executing tests locally

If you'd like to run tests on your computer, you need to do an additional step after cloning this repository:

git submodule update --init --recursive

This command downloads files which are used for testing. These files are stored in a separate repository. 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 (installing git-lfs locally with --skip-smudge option is required to solve these problems).

Note: You can also use "Utils/prepare-workspace-macos.sh" script from the repository, which not only downloads test files but also downloads dependencies.

Performace

Usage of whole module optimizations is recommended for best performance. These optimizations are enabled by default for Release configurations.

Tests Results document contains results of benchmarking of various functions.

Why?

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 "unfriendly" to users. This project attempts to provide missing (and sometimes existing) functionality through unified API, which is easy to use and remember.

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 Linux.

Future plans

  • Performance...
  • Better Deflate compression.
  • Something else...

References

Github

link
Stars: 25
Help us keep the lights on

Dependencies

Used By

Total: 1

Releases

4.3.0 - Apr 29, 2018

ZIP Custom Extra Fields

ZIP format provides capabilities to define third-party extra fields, so it is impossible for SWCompression to support all possible extra fields. In this update several APIs were added which allow users to define their own extra fields (aka "custom extra fields") and make SWCompression recognize them. All extra fields previously supported by SWCompression (aka "standard extra fields") are still supported.

  • Added ZipExtraField protocol.
  • Added ZipExtraFieldLocation enum.
  • Added ZipContainer.customExtraFields property.
  • Added ZipEntryInfo.customExtraFields property.

To add support of a custom extra field one must first create a type which conforms to ZipExtraField protocol. Then it must be added to ZipContainer.customExtraFields dictionary with the key equal to the id property of the type being added. If during execution of open(container:) or info(container:) functions custom extra field is found it will be processed using initializer of the provided type and stored in ZipEntryInfo.customExtraFields property of entry where this extra field was found.

Note: It is impossible to define custom extra field with the same ID as any of the standard extra fields and make SWCompression use user-defined extra field instead of the standard one (i.e. SWCompression first checks if ID is one of the standard IDs and then tries to find it in ZipContainer.customExtraFields dictionary).

TAR Formats

  • Added TarContainer.Format enum which represents various formats of TAR containers.
  • Added TarContainer.formatOf(container:) function which returns format of the TAR container.
  • Added -f, --format option to swcomp's tar command which prints format of the TAR container.

Comment: In the context of TAR containers "format" means a set of extensions to the basic TAR container layout which must be supported to successfully process given container.

Benchmark changes

  • Number of benchmark iterations increased from 6 to 10.
  • Benchmarks now have a zeroth iteration which is excluded from averages.

Comment: For some reason when benchmarked functions are being executed for the first time they perform significantly worse than any of the following iterations. So it was decided to drop this "zeroth" iteration from calculating of averages. This change, of course, artificially improves benchmark results, but, hopefully, makes them more reliable. On the other hand, the increase in number of iterations aims to improve accuracy of benchmarks in general.

Other changes

  • Updated to support Swift 4.1.
  • Minuimum required version of BitByteData is now 1.2.0.
  • Added TarEntryInfo.compressionMethod property which is always equal to .copy.
  • Added documenation for Container.Entry and ContainerEntry.Info associated types.
  • Reverted "disable symbol stripping" change from 4.2.0 update, since underlying problem was fixed in Carthage.

4.3.0-test.2 - Apr 20, 2018

In this test release a new function and enum have been added to TarContainer which provide information about formats of TAR containers.

4.3.0-test.1 - Apr 10, 2018

This is the first test release of 4.3.0 which includes support for ZIP custom extra fields, updates for Swift 4.1 and upcoming 1.2.0 release of BitByteData.

4.2.2 - Mar 17, 2018

  • Fixed a problem when in some rare cases (when SevenZipSubstreamInfo isn't found in a container) 7-Zip Container entries were skipped during open(container:), instead of being appended with nil data to the result array, as was always intended.
  • Fixed a crash in SevenZipContainer.info(container:) when either entry's size or CRC32 isn't present in the container.
  • Updated documentation.

4.2.1 - Mar 8, 2018

  • All 7-Zip minor format versions from 1 to 4 are now accepted (previously, it was only 4; for all other versions SevenZipError.wrongFormatVersion was thrown).