Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Unable to get Windows to read certain deflated files #99

Closed
pcrockett opened this issue Mar 3, 2019 · 4 comments
Closed

Unable to get Windows to read certain deflated files #99

pcrockett opened this issue Mar 3, 2019 · 4 comments

Comments

@pcrockett
Copy link

If I create a zip file with a subdirectory, Windows won't properly read that subdirectory. Any loose files sitting in the root of the archive can be extracted just fine, but trying to extract a file in a subdirectory yields an error.

The following will generate a zip file that reproduces this behavior:

use std::fs::{ OpenOptions };
use std::io::Write;
use zip::{ CompressionMethod, ZipWriter };
use zip::write::FileOptions;

fn main() {
    let archive = OpenOptions::new()
        .create_new(true)
        .write(true)
        .open("temp.zip")
        .unwrap();

    let mut writer = ZipWriter::new(archive);

    let options = FileOptions::default()
        .compression_method(CompressionMethod::Deflated);
    writer.start_file("noproblem.txt", options).unwrap();
    writer.write_all(b"Windows can extract this file just fine.").unwrap();

    let options = FileOptions::default();
    writer.add_directory("subdir", options).unwrap();

    let options = FileOptions::default()
        .compression_method(CompressionMethod::Deflated);
    writer.start_file("subdir/problem.txt", options).unwrap();
    writer.write_all(b"Windows will show an error when trying to extract this file.").unwrap();

    writer.flush().unwrap();
}

And here is the error I'm encountering when trying to drag / drop the subdir/problem.txt file onto my Desktop:

Unexpected Error

Running Windows 10 Home edition. 7zip extracts this just fine.

I recognize this may be a problem with Windows' built-in zip file support, but then again I've never encountered a zip file that didn't work out of the box with Windows.

@mvdnes
Copy link
Collaborator

mvdnes commented Mar 4, 2019

This is a very strange issue indeed! I have not been able to find the exact cause just yet, but it does not seem to be a related to it being a file in a subdirectory. If you change the contents of the subdir/noproblem.txt file to

writer.write_all(b"Windows will show an error when trying to extract this file.\r\nHowever, it has no problems with it after this modification.\r\n").unwrap();

then it will extract just fine. The issue also seems to be gone when changing the compression method to Stored.
It seems that it might be a problem with the compression. Some further investigation is definitely needed.

@mvdnes mvdnes changed the title Unable to get Windows to read zip files with subdirectories Unable to get Windows to read certain deflated files Mar 7, 2019
@mvdnes
Copy link
Collaborator

mvdnes commented Mar 7, 2019

So I have been investigating this issue, and the problem seems to be in the deflate stream of certain small files. I will try to get it resolved with the people of libflate.

Technical info:
It seems that Windows does not like a flate stream with zero distance codes. When manually adding a distance code table without modifying the LZ77 stream the file is perfectly readable on Windows. The RFC does seem to allow an empty distance table, so I am not sure who is at fault.

@mvdnes
Copy link
Collaborator

mvdnes commented Mar 10, 2019

This should be fixed in version 0.5.1.
Thanks for the report, and if you still have issues feel free to reopen this.

@pcrockett
Copy link
Author

Awesome. Thanks for getting that fixed, and so fast! Fist bump! 🤜 🤛

(and thanks for the library itself)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants