GzipSwift Documentation

repository·main·Indexed 20 days ago

https://github.com/1024jp/gzipswift

A Swift framework that extends the Data type to enable gzip compression and decompression using zlib. Provides methods such as .gzipped() for compression and .gunzipped() for decompression, along with an .isGzipped property to verify data format.

Tokens
489
Snippets
3
Records
3
Agent score
20%

What's inside GzipSwift

  1. Install GzipSwift on Linux

    main

    To use GzipSwift on Linux, you must ensure zlib is installed on your system.

    1. Install the zlib development package:
    $ apt-get install zlib1g-dev
    1. Add the package to your Package.swift.

    Troubleshooting Linker Errors: If the Swift build fails with a linker error, check if libz.so exists in /usr/local/lib.

    • If it is missing, reinstall zlib.
    • If it is present, manually link the library by passing -Xlinker -L/usr/local/lib to your swift build command.
  2. Install GzipSwift via Swift Package Manager

    main

    GzipSwift is SwiftPM-compatible. Add the package to your Package.swift dependencies or directly to your Xcode project using the following specification:

    dependencies: [
        .package(name: "Gzip", url: "https://github.com/1024jp/GzipSwift", from: Version(7, 0, 0)),
    ],
  3. Compress and decompress data with GzipSwift

    main

    GzipSwift extends the Data type to provide easy compression and decompression methods using zlib.

    Compression

    Use .gzipped() to compress data. You can specify a compression level, such as .bestCompression, to optimize the output.

    Decompression

    Use .gunzipped() to decompress data. It is recommended to check the .isGzipped property before attempting to decompress to ensure the data is in the correct format.

    import Gzip
    
    // gzip
    let compressedData: Data = try data.gzipped()
    let optimizedData: Data = try data.gzipped(level: .bestCompression)
    
    // gunzip
    let decompressedData: Data
    if data.isGzipped {
        decompressedData = try data.gunzipped()
    } else {
        decompressedData = data
    }