A simple rust library to read and write Zip archives, which is also my pet project for learning Rust

Related tags

Compression rust-zip
Overview

rust-zip Build Status

A simple rust library to read and write Zip archives, which is also my pet project for learning Rust. At the moment you can list the files in a Zip archive, as well as extracting them if they are either stored (uncompressed) or deflated, but I plan to add write support soon.

A simple example

ZipReader { do_or_die!(zip::ZipReader::open(&Path::new(file))) } fn output_file(file: &str)->File{ do_or_die!(File::create(&Path::new(file))) } fn zipped_file_info(zip: &mut ZipReader , file: &str) -> FileInfo{ do_or_die!(zip.info(file)) } fn list_content(reader: &mut ZipReader )->(){ for file in reader.files(){ let (year, month, day, hour, minute, second) = file.last_modified_datetime; let mod_time = format!("{:04}-{:02}-{:02} {:02}:{:02}:{:02}", year, month, day, hour, minute, second); println!("{} ({}): bytes: {:10}, compressed: {:10}", file.name, mod_time, file.compressed_size, file.uncompressed_size); } } fn extract_file(zip: &mut ZipReader , file: &str)->(){ let mut out = output_file(file); let info = zipped_file_info(zip, file); do_or_die!(zip.extract(&info, &mut out)); } fn print_usage(this: &str)->(){ println!("Usage: {} [file.zip] [file_to_extract]", this); } ">
#![feature(core, os, io, path)]

extern crate zip;

use std::os;
use std::old_io::File;
use zip::ZipReader;
use zip::fileinfo::FileInfo;

fn main() {
    let args = os::args();
    match args.len(){
        2 => list_content(&mut zip_file(&args[1][])),
        3 => extract_file(&mut zip_file(&args[1][]), &args[2][]),
        _ => print_usage(&args[0][])
    }
}

macro_rules! do_or_die{
    ($expr:expr) => (match $expr {
        Ok(val) => val,
        Err(err) => {println!("{}",err); panic!()}
    })
}

fn zip_file(file: &str) -> ZipReader
       {
    
       do_or_die!(zip
       ::ZipReader
       ::
       open(
       &
       Path
       ::
       new(file)))
}


       fn 
       output_file(file: 
       &
       str)->File{
    
       do_or_die!(File
       ::
       create(
       &
       Path
       ::
       new(file)))
}


       fn 
       zipped_file_info(zip: 
       &
       mut ZipReader
       
        , file: 
        &
        str) -> FileInfo{
    
        do_or_die!(zip.
        info(file))
}


        fn 
        list_content(reader: 
        &
        mut ZipReader
        
         )->(){
    
         for file 
         in reader.
         files(){
        
         let (year, month, day, hour, minute, second) 
         = file.last_modified_datetime;
        
         let mod_time 
         = 
         format!(
         "{:04}-{:02}-{:02} {:02}:{:02}:{:02}", year, month, day, hour, minute, second);
        
         println!(
         "{} ({}): bytes: {:10}, compressed: {:10}",
            file.name, mod_time, file.compressed_size, file.uncompressed_size);
    }
}


         fn 
         extract_file(zip: 
         &
         mut ZipReader
         
          , file: 
          &
          str)->(){
    
          let 
          mut out 
          = 
          output_file(file);
    
          let info 
          = 
          zipped_file_info(zip, file);
    
          do_or_die!(zip.
          extract(
          &info, 
          &
          mut out));
}


          fn 
          print_usage(this: 
          &
          str)->(){
    
          println!(
          "Usage: {} [file.zip] [file_to_extract]", this);
}

         
        
       
      

TODO

  • Learn more Rust
  • Write support
  • Create a proper set of tests
  • Support advanced features (more compression methods, ZIP64, encryption, multiple volumes...)
You might also like...
A Brotli implementation in pure and safe Rust

Brotli-rs - Brotli decompression in pure, safe Rust Documentation Compression provides a Read-struct to wrap a Brotli-compressed stream. A consumer

Brotli compressor and decompressor written in rust that optionally avoids the stdlib

rust-brotli What's new in 3.2 into_inner conversions for both Reader and Writer classes What's new in 3.0 A fully compatible FFI for drop-in compatibi

DEFLATE, gzip, and zlib bindings for Rust

flate2 A streaming compression/decompression library DEFLATE-based streams in Rust. This crate by default uses the miniz_oxide crate, a port of miniz.

Like pigz, but rust - a cross platform, fast, compression and decompression tool.

🦀 crabz Like pigz, but rust. A cross platform, fast, compression and decompression tool. Synopsis This is currently a proof of concept CLI tool using

A Rust application that compress files and folders

Quick Storer This is a Rust application that compress files and folders. Usage Download or build the binary and place it on your desktop, or any other

Lossless compressor and decompressor for numerical data using quantiles
Lossless compressor and decompressor for numerical data using quantiles

This rust library compresses and decompresses sequences of numerical data very well. It currently supports the following data types: i32, i64, u32, u64, f32, f64. Smaller data types like i16 can be efficiently compressed by casting to i32. Timestamp support may come soon in the future.

Obvious Unified Compression Helper is a CLI tool to help you compress and decompress files of several formats

Ouch! ouch stands for Obvious Unified Compression Helper and is a CLI tool to help you compress and decompress files of several formats. Features Usag

libbz2 (bzip2 compression) bindings for Rust

bzip2 Documentation A streaming compression/decompression library for rust with bindings to libbz2. # Cargo.toml [dependencies] bzip2 = "0.4" License

A Rust implementation of the Zopfli compression algorithm.

Zopfli in Rust This is a reimplementation of the Zopfli compression tool in Rust. I have totally ignored zopflipng. More info about why and how I did

Comments
  • fail -> panic

    fail -> panic

    Sorry I broke your code with rust-lang/rust#17894 ! Here's a fix :heart:

    (this is a semi-automatic PR, so sorry if it's not perfect. Let me know and I'll fix any problems.)

    opened by steveklabnik 0
Owner
Kang Seonghoon
Yet another procrastinating programmer
Kang Seonghoon
An extremely fast alternative to zip which is written in rust.

Zap Compress and/or encrypt folders fast. Like, really fast. or as some say, blazingly fast. Installation To install Zap, run the following command fr

null 37 Nov 9, 2022
A library to create zip files on a non-seekable writer

A library to create zip files on a non-seekable writer

nyantec GmbH 2 Mar 17, 2022
A utility that can download JavaScript and TypeScript module graphs and store them locally in a special zip file.

eszip A utility that can download JavaScript and TypeScript module graphs and store them locally in a special zip file. To create a new archive: > esz

Deno Land 162 Dec 24, 2022
Zip implementation in Rust

zip-rs Documentation Info A zip library for rust which supports reading and writing of simple ZIP files. Supported compression formats: stored (i.e. n

null 549 Jan 4, 2023
SIMD Floating point and integer compressed vector library

compressed_vec Floating point and integer compressed vector library, SIMD-enabled for fast processing/iteration over compressed representations. This

Evan Chan 56 Nov 24, 2022
Convenience library for reading and writing compressed files/streams

compress_io Convenience library for reading and writing compressed files/streams The aim of compress_io is to make it simple for an application to sup

Simon Heath 0 Dec 16, 2021
Simple NoNG songs manager for GD, written in Rust.

nong-manager Simple NoNG songs manager for GD, written in Rust. Powered by Song File Hub (https://songfilehub.com/home) How to use Enter song ID that

Alexander Simonov 4 May 13, 2023
Zopfli Compression Algorithm is a compression library programmed in C to perform very good, but slow, deflate or zlib compression.

Zopfli Compression Algorithm is a compression library programmed in C to perform very good, but slow, deflate or zlib compression.

Google 3.2k Jan 6, 2023
Fastest Snappy compression library in Node.js

snappy !!! For [email protected] and below, please go to node-snappy. More background about the 6-7 changes, please read this, Thanks @kesla . ?? Help me to

LongYinan 103 Jan 2, 2023
Basic (and naïve) LZW and Huffman compression algorithms in Rust.

Naive implementation of the LZW and Huffman compression algorithms. To run, install the Rust toolchain. Cargo may be used to compile the source. Examp

Luiz Felipe Gonçalves 9 May 22, 2023