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

unzip doesn't work if the directory entry doesn't come before any file entry in the directory #29

Open
leopf opened this issue Jul 3, 2021 · 2 comments

Comments

@leopf
Copy link

leopf commented Jul 3, 2021

While dealing with a filesystem error when unpacking a zip to a directory, I saw the comment "hopefully the directory is prior to any files inside it!" in line 198 in mod.ts.

In my case the directory entry for a file was missing completely. I could however still open the zip with any zip tool on my computer.

I created a solution with a helper function as shown:

import { dirname, resolve } from "https://deno.land/[email protected]/path/mod.ts";
import { JSZip } from "https://deno.land/x/[email protected]/mod.ts";

export async function unpackJSZip(zip: JSZip, dir = ".") {
    const createdDirs = new Set<string>();

    for (const fileEntry of zip) {
        const filePath = resolve(dir, fileEntry.name);

        const dirPath = fileEntry.dir ? filePath : dirname(filePath);

        if (!createdDirs.has(dirPath)) {
            await Deno.mkdir(dirPath, { recursive: true });
            createdDirs.add(dirPath);
        }

        if (!fileEntry.dir) {
            const content = await fileEntry.async("uint8array");
            // TODO pass WriteFileOptions e.g. mode
            await Deno.writeFile(filePath, content);
        }
    }
}

This fixed the bug with a minimal performance penalty.

@hayd
Copy link
Owner

hayd commented Jul 3, 2021

Thanks for finding this and investigating!
So a test case for this is a zip file with only foo/bar.txt (no directory) ?

Happy to accept a PR 💪 (else i will get around to this at some point).

@leopf
Copy link
Author

leopf commented Jul 3, 2021

After some further investigation I found out this is a problem with adding files, which have paths including backslashes. I therefore added a test for this.

Pretty much every zip tool, including unzip handles this by creating the directory. But in linux backslashes are allowed in filenames, which could mean breaking changes. On windows the unzip method in the current implementation fails in this case.

Furthermore, I suggest adding an option to the add file method, which controls if the backslahes in the filepath are replaced with forward slashes, since this produces warnings in linux' unzip.

All these changes are implemented in my following PR.

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

No branches or pull requests

2 participants