#file-header #directory #central #zip-archive #local-file #data #synthetic

synthzip

constructs synthetic Central Directories when missing from otherwise-valid ZIP data

1 unstable release

0.1.0 Nov 28, 2023

#299 in Compression

Download history 3/week @ 2024-02-18 14/week @ 2024-02-25 5/week @ 2024-03-03 3/week @ 2024-03-10 20/week @ 2024-03-31 37/week @ 2024-04-07

57 downloads per month
Used in orphist

MIT license

35KB
782 lines

synthzip Latest Version

What is it?

A rust library for constructing a synthetic Central Directory when you have ZIP data that has a Local File Header (LFH), but no Central Directory File Header (CDFH), or End of Central Directory (EOCD) data.

This is a somewhat niche library. You probably won't need it (directly), unless you happen to be doing something unusual. But if you are, it may be exactly what you need to do that one weird thing.

How do I obtain this majestic tool?

Run the following Cargo command in your project directory (assuming you have cargo-edit installed):

cargo add synthzip

Or add the following line to your Cargo.toml (in the [dependencies] array):

synthzip = "^ 0.1"

How do I use it?

fn main() {
  // This can be any source that implements Read + Seek, and reads a valid ZIP
  // Local File Header, compressed data, and (optional) trailing Data Descriptor.
  let mut input = std::io::Cursor::new(Vec::new());

  // read the data into an entry.
  let entry = synthzip::Entry::read(&mut input).expect("failed to read zip entry from input");

  // create a new (empty) CentralDirectory
  let mut index = synthzip::CentralDirectory::new();

  // add the Entry to the CentralDirectory. This will create a corresponding ZIP
  // Central Directory File Header and update the End of Central Directory appropriately.
  index.add(&entry)

  // this can be any destination that implements Write + Seek.
  let mut output = std::fs::File::create("/path/for/output.zip").expect("failed to create output file");

  // write the entry to the output destination.
  entry.write(&mut output).expect("failed to write zip entry to output");

  // write the Central Directory after the entry data.
  index.write(&mut output).expect("failed to write central directory to output");

  /// flush the output, if you're so inclined.
  output.flush().unwrap();
}

License

synthzip is available under the MIT License. See LICENSE.txt for the full text.

While the license is short, it's still written in fancy lawyer-speak. If you prefer more down-to-earth language, consider the following:

  • tl;drLegal has a simple visual summary available here.
  • FOSSA has a more in-depth overview available here.

Dependencies

~2MB
~41K SLoC