DataCompression

repository·master·Indexed 43 days ago

https://github.com/mw99/datacompression

A Swift wrapper around libcompression that extends the Data type with compression and decompression methods. It supports algorithms including GZIP, LZ4, LZFSE, LZMA, and ZLIB, and provides shortcuts for Deflate, GZIP, and ZIP formats. Also includes methods for calculating CRC32 and Adler32 checksums. Supports iOS 9.0+, macOS 10.11+, tvOS 9.0+, watchOS 2.0+, and visionOS (v3.9.0+).

Tokens
1.3K
Snippets
5
Records
5
Agent score
5%

What's inside DataCompression

  1. Install DataCompression

    master

    You can integrate DataCompression into your Xcode project using CocoaPods, Carthage, Swift Package Manager, or by manually adding the source file.

    Requirements:

    • iOS >= 9.0
    • macOS >= 10.11
    • tvOS >= 9.0
    • watchOS >= 2.0
    • visionOS (supported in v3.9.0+)

    After installation, add import DataCompression to your Swift files.

    ### CocoaPods
    
    Add to your `Podfile`:
    ```ruby
    target '<your_target_name>' do
        pod 'DataCompression'
    end

    Then run:

    $ pod install

    Carthage

    Add to your Cartfile:

    gitub "mw99/DataCompression"

    Swift Package Manager

    Add to your Package.swift:

    import PackageDescription
    
    let package = Package(
        name: "<your_package_name>",
        dependencies: [
            .Package(url: "https://github.com/mw99/DataCompression.git", majorVersion: 3)
        ]
    )

    Then run:

    $ swift build

    Manual

    Drag and drop Sources/DataCompression.swift directly into your Xcode project.

  2. Decompress a GZIP file created via command line

    master

    You can compress a file using the system gzip tool and then decompress it within your Swift application using the .gunzip() extension.

    # Compress file.txt using command line
    gzip -9 file.txt
    # This creates file.txt.gz
    // Decompress in Swift
    let compressedData = try? Data(contentsOf: URL(fileURLWithPath: "/path/to/your/file.txt.gz"))
    
    if let uncompressedData = compressedData?.gunzip() {
        print(String(data: uncompressedData, encoding: .utf8) ?? "Can't decode UTF-8")
    }
  3. Calculate CRC32 and Adler32 checksums

    master

    The Data type is extended with methods to calculate common checksums for data integrity verification.

    • .crc32(): Returns the CRC32 checksum.
    • .adler32(): Returns the Adler32 checksum.
    let data = "The quick brown fox jumps over the lazy dog".data(using: .utf8)!
    let crc32 = data.crc32()
    let adler32 = data.adler32()
    print("crc32: \(crc32), adler32: \(adler32)")
  4. Use container format shortcuts (GZIP, ZIP, Deflate)

    master

    The library provides high-level shortcuts for common container formats on the Data type:

    • Deflate: Use .deflate() and .inflate() for ZLIB deflate raw ([RFC-1951]).
    • GZIP: Use .gzip() and .gunzip() for GZIP format ([RFC-1952]). Note: GZIP data is ~18 bytes larger than raw deflate and includes a CRC32 checksum.
    • ZIP: Use .zip() and .unzip() for ZIP format ([RFC-1950]). Note: ZIP data is ~6 bytes larger than raw deflate and includes an Adler32 checksum.
    // GZIP Example
    let data = "https://www.ietf.org/rfc/rfc1952.txt".data(using: .utf8)!
    let gzipped = data.gzip()
    let gunzipped = gzipped.gunzip()
    assert(data == gunzipped)
    
    // ZIP Example
    let zipped = data.zip()
    let unzipped = zipped.unzip()
    assert(data == unzipped)
    
    // Deflate Example
    let deflated = data.deflate()
    let inflated = deflated.inflate()
    assert(data == inflated)
  5. Compress and decompress data using algorithms

    master

    Use the compress(withAlgorithm:) and decompress(withAlgorithm:) methods on Data to perform compression using various algorithms.

    Supported Data.CompressionAlgorithm values:

    • .zlib (ZLIB deflate stream [RFC-1950])
    • .zlibRaw (ZLIB deflate raw [RFC-1951])
    • .lzfse
    • .lz4
    • .lzma
    • .gzip (GZIP format [RFC-1952])
    let raw: Data = "There is no place like 127.0.0.1".data(using: .utf8)!
    
    // Compress
    let compressedData = raw.compress(withAlgorithm: .lzfse)
    
    // Decompress
    if let uncompressedData = compressedData.decompress(withAlgorithm: .lzfse) {
        print(String(data: uncompressedData, encoding: .utf8)!)
    }