Skip to content

Commit

Permalink
Merge pull request #39 from fasterthanlime/num-enum
Browse files Browse the repository at this point in the history
Use num-enum crate, recognize more compression methods
  • Loading branch information
fasterthanlime authored Jan 26, 2024
2 parents 149e6c1 + 601ea02 commit 00bb71c
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 40 deletions.
79 changes: 79 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 8 additions & 6 deletions crates/jean/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ fn do_main(cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
println!("Comment:\n{}", comment);
}
let has_zip64 = archive.entries().any(|entry| entry.inner.is_zip64);
println!("{}", if has_zip64 { "Zip64" } else { "Zip32" });
if has_zip64 {
println!("Found Zip64 end of central directory locator")
}

let mut creator_versions = HashSet::<rc_zip::Version>::new();
let mut reader_versions = HashSet::<rc_zip::Version>::new();
Expand Down Expand Up @@ -131,6 +133,7 @@ fn do_main(cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
Commands::Ls { zipfile, verbose } => {
let zipfile = File::open(zipfile)?;
let reader = zipfile.read_zip()?;
info(&reader);

for entry in reader.entries() {
print!(
Expand All @@ -157,13 +160,12 @@ fn do_main(cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
print!("\t{target}", target = target);
}

if let Some(comment) = entry.comment() {
print!("\t{comment}", comment = comment);
}
print!("\t{:?}", entry.method());
if entry.inner.is_zip64 {
print!("\tZip64");
} else {
print!("\tZip32");
}
if let Some(comment) = entry.comment() {
print!("\t{comment}", comment = comment);
}
}
println!();
Expand Down
1 change: 1 addition & 0 deletions crates/rc-zip/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ oem_cp = "2.0.0"
thiserror = "1.0.56"
chardetng = "0.1.17"
flate2 = { version = "1.0.28", optional = true }
num_enum = "0.7.2"

[features]
default = ["sync", "file", "deflate"]
Expand Down
69 changes: 36 additions & 33 deletions crates/rc-zip/src/format/archive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::format::*;
use num_enum::{FromPrimitive, IntoPrimitive};

/// An Archive contains general information about a zip files,
/// along with a list of [entries][StoredEntry].
Expand Down Expand Up @@ -278,44 +279,46 @@ impl StoredEntry {
///
/// However, in the wild, it is not too uncommon to encounter [Bzip2][Method::Bzip2],
/// [Lzma][Method::Lzma] or others.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, IntoPrimitive, FromPrimitive)]
#[repr(u16)]
pub enum Method {
/// No compression is applied
Store,
Store = 0,

/// [DEFLATE (RFC 1951)](https://www.ietf.org/rfc/rfc1951.txt)
Deflate,
Deflate = 8,

/// [DEFLATE64](https://deflate64.com/)
Deflate64 = 9,

/// [BZIP-2](https://github.com/dsnet/compress/blob/master/doc/bzip2-format.pdf)
Bzip2,
Bzip2 = 12,

/// [LZMA](https://github.com/jljusten/LZMA-SDK/blob/master/DOC/lzma-specification.txt)
Lzma,
/// A compression method that isn't supported by this crate.
///
/// The original u16 is preserved.
Unsupported(u16),
}
Lzma = 14,

impl From<u16> for Method {
fn from(m: u16) -> Self {
use Method::*;
match m {
0 => Store,
8 => Deflate,
12 => Bzip2,
14 => Lzma,
_ => Unsupported(m),
}
}
}
/// [zstd](https://datatracker.ietf.org/doc/html/rfc8878)
Zstd = 93,

impl From<Method> for u16 {
fn from(m: Method) -> Self {
use Method::*;
match m {
Store => 0,
Deflate => 8,
Bzip2 => 12,
Lzma => 14,
Unsupported(m) => m,
}
}
/// [MP3](https://www.iso.org/obp/ui/#iso:std:iso-iec:11172:-3:ed-1:v1:en)
Mp3 = 94,

/// [XZ](https://tukaani.org/xz/xz-file-format.txt)
Xz = 95,

/// [JPEG](https://jpeg.org/jpeg/)
Jpeg = 96,

/// [WavPack](https://www.wavpack.com/)
WavPack = 97,

/// [PPMd](https://en.wikipedia.org/wiki/Prediction_by_partial_matching)
Ppmd = 98,

/// AE-x encryption marker (see Appendix E of appnote)
Aex = 99,

/// A compression method that isn't recognized by this crate.
#[num_enum(catch_all)]
Unrecognized(u16),
}
1 change: 0 additions & 1 deletion crates/rc-zip/src/format/directory_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ impl DirectoryHeader {
needs_uncompressed_size: self.uncompressed_size == !0u32,
needs_header_offset: self.header_offset == !0u32,
};
trace!("extra field settings: {:#?}", settings);

let mut slice = &self.extra.0[..];
while !slice.is_empty() {
Expand Down
Binary file added crates/rc-zip/testdata/test-zips/found-me-bzip2.zip
Binary file not shown.
Binary file not shown.
Binary file added crates/rc-zip/testdata/test-zips/found-me-lzma.zip
Binary file not shown.

0 comments on commit 00bb71c

Please sign in to comment.