Skip to content

A Swift framework for working with compression, archives and containers.

License

Notifications You must be signed in to change notification settings

BoltDocs/SWCompressionTAR

Repository files navigation

SWCompression

GitHub license CocoaPods Swift 3 Build Status codecov

CocoaPods Carthage compatible

A framework which contains implementations of (de)compression algorithms and functions which parse various archives and containers.

Developed with Swift.

Why have you made this framework?

There are a couple of reasons for this.

The main reason is that it is very educational and somewhat fun.

Secondly, if you are a Swift developer and you want to compress/decompress something in your project you have to use either wrapper around system libraries (which is probably written in Objective-C) or you have to use built-in Compression framework. You might think that last option is what you need, but, frankly that framework has a bit complicated API and somewhat questionable choice of supported compression algorithms. And yes, it is also in Objective-C.

And here comes SWCompression: no Objective-C, pure Swift.

Features

  • Containers:
    • ZIP
    • TAR
  • Decompression algorithms:
    • LZMA/LZMA2
    • Deflate
    • BZip2
  • Compression algorithms:
    • Deflate
  • Archives:
    • XZ
    • GZip
    • Zlib
  • Platform independent.
  • Written with Swift only.

By the way, it seems like GZip, Deflate and Zlib implementations are specification compliant.

Installation

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

CocoaPods

Add to your Podfile pod 'SWCompression'.

There are several sub-podspecs in case you need only parts of framework's functionality. Available subspecs:

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

You can add some or all of them instead of pod 'SWCompression'

Also, do not forget to include use_frameworks! line in your Podfile.

To complete installation, run pod install.

Note: Actually, there is one more subspec (SWCompression/Common) but it does not contain any end-user functions. It is included in every other subspec and should not be specified directly in Podfile.

Carthage

Add to your Cartfile github "tsolomko/SWCompression".

Then run carthage update.

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

Swift Package Manager

Add to you package dependecies .Package(url: "https://github.com/tsolomko/SWCompression.git"), for example like this:

import PackageDescription

let package = Package(
    name: "PackageName",
    dependencies: [
        .Package(url: "https://github.com/tsolomko/SWCompression.git", majorVersion: 3)
    ]
)

More info about SPM you can find at Swift Package Manager's Documentation.

SWCompression/ZIP and compression methods

Deflate is a default compression method of ZIP containers.

This means, that if you use CocoaPods, when installing SWCompression/ZIP it will install SWCompression/Deflate as a dependency.

However, ZIP containers can also support LZMA and BZip2. So if you want to enable them in your Pods configuration you need to include SWCompression/LZMA and/or SWCompression/Deflate.

If you use Carthage or Swift Package Manager you always have the full package, and ZIP will be built with both BZip2 and LZMA support.

Usage

Basics

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

let data = try! Data(contentsOf: URL(fileURLWithPath: "path/to/file"),
                     options: .mappedIfSafe)
let decompressedData = try? Deflate.decompress(data: data)

Note: It is highly recommended to specify Data.ReadingOptions.mappedIfSafe, especially if you are working with large files, so you don't run out of system memory.

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)

One final note: every SWCompression function can throw an error and you are responsible for handling them.

Documentation

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

Handling Errors

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 = try Data(contentsOf: URL(fileURLWithPath: "path/to/file"),
                      options: .mappedIfSafe)
  let decompressedData = XZArchive.unarchive(archive: data)
} catch let error as XZError {
  <handle XZ related error here>
} catch let error {
  <handle all other errors here>
}

Sophisticated example

There is a small program, swcomp, which uses SWCompression for unarchiving several types of archives.

Why is it so slow?

Version 2.0 came with a great performance improvement. Just look at the Tests Results. So if it's slow the first thing you should do is to make sure you are using version >= 2.0.

Is it still slow? Maybe you are compiling SWCompression not for 'Release' but with 'Debug' build configuration? For some reason, when framework is built for 'Debug' its performance significantly worse. You can once again check test results if you want to convince yourself that this is the case.

Finally, SWCompression's code is not as optimized as original C/C++ versions of corresponding algorithms, so some difference in speed is expected.

To sum up, it is highly recommended to build SWCompression with 'Release' configuration and use the latest version (at least 2.0).

Future plans

  • Better Deflate compression.
  • Support for additional attributes in containers.
  • 7zip containers.
  • BZip2 compression.
  • Something else...

References

About

A Swift framework for working with compression, archives and containers.

Resources

License

Stars

Watchers

Forks

Languages