guzba / zippy

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

Create zipfile in memory. #35

Closed enthus1ast closed 3 months ago

enthus1ast commented 2 years ago

It would be nice to have a procedure that just returns the binary of a zipfile instead of writing to disk.

guzba commented 2 years ago

This 100% makes sense. The current zip archive and tarball implementation is pretty basic, I mostly wanted to learn how they work and figured a basic thing is better than nothing. I now know how to do both much better and will be making improvements over time, including this option to get a zip archive blob without writing to disk.

enthus1ast commented 2 years ago

Should i leave this issue open for documenting/referencing?

guzba commented 2 years ago

Yep that's fine, can use this for updates unless you'd prefer to close it.

mashingan commented 2 years ago

For easiest changes without changing much the implementation is by changing writeZipArchive to $ (which returns string) or write (name can be vary) which returns seq[uint8] , change it to return the data instead of directly writing it to file, and have the actual writeZipArchive reuses it:

proc writeZipArchive*(archive: ZipArchive, path: string) =
    path.writeFile($archive) # or path.writeFile(archive.write)

This way we still have the same writeZipArchive works as is.

guzba commented 3 months ago

I had a need for this myself recently so I wrote a new zip archive writer that just returns the archive bytes as a string. See this example: https://github.com/guzba/zippy/blob/master/examples/ziparchive_create.nim

This is about as simple of an API I could come up with for a quick zip archive creater. It does mean many options are not possible, such as controlling file modified timestamps etc. But for many scenarios this simple creater should work very well.