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

Failed to read zip archives larger than 4 GB that are created by the crate #192

Open
ChieloNewctle opened this issue Jun 12, 2024 · 4 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@ChieloNewctle
Copy link

ChieloNewctle commented Jun 12, 2024

Describe the bug
Failed to read files with this crate inside zip archives that are also created by the crate.
The error Invalid local file header shows when reading some files around 4 GB border.

To Reproduce
Steps to reproduce the behavior:

  1. Create a zip archive larger than 4 GB with multiple files that are smaller than 2 GB.
  2. Read the files inside the archive.
  3. The error will show when reading a file around 4 GB.

Expected behavior
All files can be read properly.

Workaround
Archives created by other zip command line tools can be read with this crate properly.

@ChieloNewctle ChieloNewctle added the bug Something isn't working label Jun 12, 2024
@cosmicexplorer
Copy link
Contributor

Which version of the crate demonstrates this behavior? Could I ask you to provide a code snippet I can compile and run which exhibits the bug?

@ChieloNewctle
Copy link
Author

Which version of the crate demonstrates this behavior? Could I ask you to provide a code snippet I can compile and run which exhibits the bug?

Sure thing. Here is an example using zip 2.1.3 with the dependency fixed:

[dependencies]
zip = "=2.1.3"

Archiving the zip file:

use std::{
    fs::File,
    io::{BufWriter, Read, Write},
};

use zip::{write::SimpleFileOptions, CompressionMethod, ZipWriter};

const FILE_SIZE_1GB: usize = 1024 * 1024 * 1024;
const NUM_FILES: usize = 6;

#[derive(Debug, Default)]
pub struct FakeFile<const SIZE: usize>(usize);

impl<const SIZE: usize> Read for FakeFile<SIZE> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        let chunk_size = buf.len().min(SIZE.saturating_sub(self.0));
        buf[..chunk_size].fill(0);
        self.0 += chunk_size;
        Ok(chunk_size)
    }
}

fn main() -> zip::result::ZipResult<()> {
    let path = std::env::args_os()
        .nth(1)
        .expect("please specify file name");

    let file = File::create(path)?;
    let mut writer = ZipWriter::new(BufWriter::new(file));

    let options = SimpleFileOptions::default()
        .compression_method(CompressionMethod::Stored)
        .large_file(true);
    // the error occurs regardless of whether the `large_file` flag is set.

    for i in 0..NUM_FILES {
        writer.start_file(i.to_string(), options)?;
        std::io::copy(&mut FakeFile::<FILE_SIZE_1GB>::default(), &mut writer)?;
    }

    writer.finish()?.flush()?;

    Ok(())
}

Unpacking the zip file:

use std::{
    fs::File,
    io::{BufReader, Read},
};

use zip::ZipArchive;

const BUF_SIZE_256MB: usize = 256 * 1024 * 1024;

fn main() -> zip::result::ZipResult<()> {
    let path = std::env::args_os()
        .nth(1)
        .expect("please specify file name");

    let mut archive = ZipArchive::new(BufReader::new(File::open(path)?))?;

    let file_names = archive
        .file_names()
        .map(|x| x.to_owned())
        .collect::<Vec<_>>();

    let mut buf = vec![0; BUF_SIZE_256MB];
    for name in file_names {
        let mut file = archive.by_name(&name)?;
        let mut total = 0;
        loop {
            let num = file.read(&mut buf)?;
            if num == 0 {
                break;
            }
            total += num;
        }
        println!("{name}: {total} bytes in total");
    }

    Ok(())
}

Running unpacking:

0: 1073741824 bytes in total
1: 1073741824 bytes in total
2: 1073741824 bytes in total
3: 1073741824 bytes in total
Error: InvalidArchive("Invalid local file header")

@Pr0methean Pr0methean added the help wanted Extra attention is needed label Jun 22, 2024
@LoganAMorrison
Copy link

@Pr0methean Do you have a sense of where to look for the source of this error? I hit the same issue and would like to take a stab at a solution

@onlyxuyang
Copy link

onlyxuyang commented Oct 14, 2024

@Pr0methean fixed in #248

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants