zipx

    A pure MoonBit implementation of ZIP archive extraction

    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    5 months ago
    Downloads
    126

    #wangmingfa/zipx

    A pure MoonBit implementation of ZIP archive extraction, without FFI or system commands.

    #Features

    • Pure MoonBit implementation (no FFI, no system calls)
    • DEFLATE decompression support
    • Store (no compression) support
    • CLI tool for extracting and listing ZIP files
    • Async file I/O support

    #Installation

    moon add wangmingfa/zipx

    #Usage

    #As a Library

    import { unzip_file, zip_file_open, zip_file_list } from "wangmingfa/zipx"

    // Extract all files from a ZIP archive
    let extracted : Array[(String, Bytes)] = unzip_file(zip_data)
    for entry in extracted.iter() {
    let (filename, data) = entry
    // Process extracted file...
    }

    // Or use lower-level API
    let zip = zip_file_open(zip_data)
    let files = zip_file_list(zip) // Get list of filenames
    let (filename, data) = zip_file_extract(zip, 0) // Extract by index

    #CLI Tool

    # Build the CLI tool moon build cmd/main/ # List files in a ZIP archive moon run cmd/main/ list /path/to/archive.zip # Extract files from a ZIP archive moon run cmd/main/ unzip /path/to/archive.zip ./output_dir

    #API Reference

    #unzip_file

    pub fn unzip_file(data : Bytes) -> Array[(String, Bytes)] raise

    Extracts all files from a ZIP archive. Returns an array of (filename, data) tuples.

    #zip_file_open

    pub fn zip_file_open(data : Bytes) -> ZipFile raise

    Opens a ZIP archive for reading. Returns a ZipFile handle.

    #zip_file_list

    pub fn zip_file_list(zip : ZipFile) -> Array[String]

    Returns a list of all filenames in the ZIP archive.

    #zip_file_extract

    pub fn zip_file_extract(zip : ZipFile, index : Int) -> (String, Bytes) raise

    Extracts a single file by index. Returns (filename, data) tuple.

    #inflate

    pub fn inflate(data : Bytes) -> Bytes

    Low-level DEFLATE decompression function.

    #Supported Compression Methods

    MethodDescription
    Store (0)No compression
    Deflate (8)DEFLATE compression

    #License

    Apache-2.0

    BitReader

    pub struct BitReader {
    data : Bytes
    pos : Int
    bit_pos : Int
    }

    CentralDirectoryFileHeader

    pub struct CentralDirectoryFileHeader {
    signature : UInt
    version_made_by : UInt16
    version_needed : UInt16
    flags : UInt16
    compression_method : CompressionMethod
    last_mod_time : UInt16
    last_mod_date : UInt16
    crc32 : UInt
    compressed_size : UInt
    uncompressed_size : UInt
    filename_length : UInt16
    extra_field_length : UInt16
    comment_length : UInt16
    disk_number_start : UInt16
    internal_file_attrs : UInt16
    external_file_attrs : UInt
    local_header_offset : UInt
    filename : String
    extra_field : Bytes
    comment : String
    }

    CompressionMethod

    pub enum CompressionMethod {
    Stored
    Deflated
    Unsupported(UInt16)
    }

    EndOfCentralDirectoryRecord

    pub struct EndOfCentralDirectoryRecord {
    signature : UInt
    disk_number : UInt16
    cd_start_disk : UInt16
    cd_entries_on_disk : UInt16
    cd_entries_total : UInt16
    cd_size : UInt
    cd_offset : UInt
    comment_length : UInt16
    comment : String
    }

    HuffmanTree

    pub struct HuffmanTree {
    codes : Array[Int]
    lengths : Array[Int]
    max_code_len : Int
    }

    LocalFileHeader

    pub struct LocalFileHeader {
    signature : UInt
    version_needed : UInt16
    flags : UInt16
    compression_method : CompressionMethod
    last_mod_time : UInt16
    last_mod_date : UInt16
    crc32 : UInt
    compressed_size : UInt
    uncompressed_size : UInt
    filename_length : UInt16
    extra_field_length : UInt16
    filename : String
    extra_field : Bytes
    }

    ZipFile

    pub struct ZipFile {
    data : Bytes
    eocd : EndOfCentralDirectoryRecord
    cd_entries : Array[CentralDirectoryFileHeader]
    }

    inflate

    fn inflate(data : Bytes) -> Bytes

    unzip_file

    fn unzip_file(data : Bytes) -> Array[(String, Bytes)] raise

    zip_file_extract

    fn zip_file_extract(zip : ZipFile, index : Int) -> (String, Bytes) raise

    zip_file_list

    fn zip_file_list(zip : ZipFile) -> Array[String]

    zip_file_open

    fn zip_file_open(data : Bytes) -> ZipFile raise

    Source Files