guzba / zippy

Pure Nim implementation of deflate, zlib, gzip and zip.
MIT License
246 stars 29 forks source link

Extract Zip File from memory #5

Closed byt3bl33d3r closed 3 years ago

byt3bl33d3r commented 3 years ago

Heya,

First off, thanks a ton for this library.

I'm not exactly sure what I'm doing wrong here, I have the following code:

import zippy
import zippy/ziparchives
import strformat
import os

const pyzip = slurp"../rsrc/python-3.9.1-embed-amd64.zip"
const tmpdir: string = getEnv("LOCALAPPDATA") / "Temp"

proc extractPython(): bool =
    var z = uncompress(pyzip)
    extractAll(z, tmpdir)
    return true

proc main(): void =
    echo fmt"[*] tmpdir: {tmpdir}"
    if extractPython():
        echo fmt"[*] Python extracted"

when isMainModule:
    main()

I'm cross compiling to Windows from MacOS using Mingw. The above code produces the following error when run:

image

Does zippy not support decompressing zip files this way?

Thanks

guzba commented 3 years ago

I believe the issue here is you are attempting to directly uncompres the .zip file you read with var z = uncompress(pyzip). A zip file is actually many small compressed blobs for each individual file so that does not work. All you need to do in this case is use extractAll(zipPath, tmpdir) with no uncompress beforehand. extractAll will handle the uncompressing.

Something like: extractAll("../rsrc/python-3.9.1-embed-amd64.zip", tmpdir)

byt3bl33d3r commented 3 years ago

So in this particular case pyzip is an embedded resource that gets put in memory at compile time (not the location of a file). From what I've read extractAll can only extract files from disk not from memory.

If i don't call uncompress first and just do a extractAll(pyzip, tmpdir) I get the following error:

image

guzba commented 3 years ago

Ah ok. I do not have an API for opening zip archives in memory since I didn't think that'd be something that would be needed. For now you'll need to write it to disk as a temp file somewhere to use Zippy.

guzba commented 3 years ago

Rereading, it sounds like you are embedding that zip archive into the compilation of your program? My answer about Zippy not supporting that for zip archives / tarballs is still true but obviously writing a temp file won't help.

byt3bl33d3r commented 3 years ago

Gotcha, Yeah in this particular case the ability to extract zip files from memory to disk would be very useful. I don't think there's a good library to do that in Nim. The official zip library doesn't seem to work when x-compiled to windows.

guzba commented 3 years ago

I have tagged release 0.5.4 https://github.com/guzba/zippy/releases/tag/0.5.4 which includes the PR adding support for this. Closing this issue now as I think we're good. Feel free to re-open if we're not actually ready to here just yet.