Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: new example to show how to write large files (greater than 4GB) #67

Merged
merged 19 commits into from
May 4, 2024
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions examples/write-large-file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//! Write a huge file with lots of zeros, that should compress perfectly.

fn main() -> Result<(), Box<dyn std::error::Error>> {
if !cfg!(feature = "_deflate-any") {
return Err("Please enable one of the deflate features".into());
}
let args: Vec<_> = std::env::args().collect();
if args.len() < 2 {
return Err(format!("Usage: {} <filename>", args[0]).into());
}

#[cfg(feature = "_deflate-any")]
{
let filename = &*args[1];
doit(filename)?;
}
Ok(())
}

#[cfg(feature = "_deflate-any")]
fn doit(filename: &str) -> zip::result::ZipResult<()> {
use std::io::Write;

use zip::write::SimpleFileOptions;

let file = std::fs::File::create(filename)?;
let mut zip = zip::ZipWriter::new(file);

let options = SimpleFileOptions::default()
.compression_method(zip::CompressionMethod::Deflated)
// files over u32::MAX require this flag set.
.large_file(true)
.unix_permissions(0o755);
zip.start_file("huge-file-of-zeroes", options)?;
let content: Vec<_> = std::iter::repeat(0_u8).take(65 * 1024).collect();
let mut bytes_written = 0_u64;
while bytes_written < u32::MAX as u64 {
zip.write_all(&content)?;
bytes_written += content.len() as u64;
}
zip.finish()?;
Ok(())
}
Loading