Skip to content

Commit

Permalink
Update zip dependency to version 2 (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
jturner314 authored Sep 14, 2024
1 parent 9574a2f commit 3efa590
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ndarray = "0.16"
num-complex-0_4 = { package = "num-complex", version = "0.4", optional = true }
num-traits = "0.2"
py_literal = "0.4"
zip = { version = "0.6.3", default-features = false, optional = true }
zip = { version = "2", default-features = false, optional = true }

[features]
default = ["compressed_npz", "num-complex-0_4"]
Expand Down
18 changes: 10 additions & 8 deletions src/npz.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::error::Error;
use std::fmt;
use std::io::{BufWriter, Read, Seek, Write};
use zip::result::ZipError;
use zip::write::FileOptions;
use zip::write::SimpleFileOptions;
use zip::{CompressionMethod, ZipArchive, ZipWriter};

/// An error writing a `.npz` file.
Expand Down Expand Up @@ -78,7 +78,7 @@ impl From<WriteNpyError> for WriteNpzError {
/// ```
pub struct NpzWriter<W: Write + Seek> {
zip: ZipWriter<W>,
options: FileOptions,
options: SimpleFileOptions,
}

impl<W: Write + Seek> NpzWriter<W> {
Expand All @@ -88,7 +88,7 @@ impl<W: Write + Seek> NpzWriter<W> {
pub fn new(writer: W) -> NpzWriter<W> {
NpzWriter {
zip: ZipWriter::new(writer),
options: FileOptions::default().compression_method(CompressionMethod::Stored),
options: SimpleFileOptions::default().compression_method(CompressionMethod::Stored),
}
}

Expand All @@ -99,16 +99,18 @@ impl<W: Write + Seek> NpzWriter<W> {
pub fn new_compressed(writer: W) -> NpzWriter<W> {
NpzWriter {
zip: ZipWriter::new(writer),
options: FileOptions::default().compression_method(CompressionMethod::Deflated),
options: SimpleFileOptions::default().compression_method(CompressionMethod::Deflated),
}
}

/// Creates a new `.npz` file with the specified options.
///
/// This allows you to use a custom compression method, such as [`CompressionMethod::Zstd`] or set other options.
/// This allows you to use a custom compression method, such as [`CompressionMethod::Zstd`] or
/// set other options.
///
/// Make sure to enable the `zstd` feature of the `zip` crate to use [`CompressionMethod::Zstd`] or other relevant features.
pub fn new_with_options(writer: W, options: FileOptions) -> NpzWriter<W> {
/// Make sure to enable the relevant features of the `zip` crate to use
/// [`CompressionMethod::Zstd`] or other features.
pub fn new_with_options(writer: W, options: SimpleFileOptions) -> NpzWriter<W> {
NpzWriter {
zip: ZipWriter::new(writer),
options,
Expand Down Expand Up @@ -152,7 +154,7 @@ impl<W: Write + Seek> NpzWriter<W> {
/// [`BufWriter`](std::io::BufWriter)) flush the writer, any errors that
/// occur during drop will be silently ignored. So, it's necessary to call
/// `.finish()` to properly handle errors.
pub fn finish(mut self) -> Result<W, WriteNpzError> {
pub fn finish(self) -> Result<W, WriteNpzError> {
let mut writer = self.zip.finish()?;
writer.flush().map_err(ZipError::from)?;
Ok(writer)
Expand Down

0 comments on commit 3efa590

Please sign in to comment.