Let's talk about zip files
- Posted by ghaberek (admin) Feb 21, 2022
- 2062 views
Another thing I'm working on and have half-completed (along with structs and hashes) is zip files. I've got miniz implemented in the backend and I'm exposing its API to the frontend via a sequence of pointers and define_c_func/proc.
Question is: what does a good, simple, Euphoria-like zip API look like? Let's discuss! (For reference, the miniz archive API lives here: miniz_zip.h) I'm thinking that a zip file library is basically a combination of two things: file system (dirs/files, names/sizes, etc.) and file I/O (open/close, read/write, etc.).
With that, the typical operations are going to be: creating zip files, inspecting zip files, and extracting zip files. So to start with, here's what I propose. (There will be additional routines for doing more advanced things with zip files, but I think this covers most use cases).
Zip file routines
include std/zip.e --** -- Open a zip file for reading or writing. public function zip_open( sequence filename, sequence mode, integer flags=0, atom archive_start=0, atom archive_size=0 ) --** -- Close (and if writing, finalize) a zip file. public function zip_close( atom zip, integer finalize=TRUE ) --** -- Add an entry to a zip file. public function zip_add( atom zip, sequence source_name, sequence archive_name="", integer flags=ZIP_DEFAULT_COMPRESSION, sequence comment="" ) --** -- Extract an entry from a zip file. public function zip_extract( atom zip, sequence archive_name, sequence destination_name="", integer flags=0 ) --** -- Locate an entry in a zip file. public function zip_find( atom zip, sequence filename, integer flags=0, sequence comment="" ) --** -- List the entries in a zip file, filtered by an optional pattern. public function zip_dir( atom zip, sequence pattern="" )
Creating zip files
include std/zip.e -- open zip file for writing atom zip = zip_open( "test.zip", "w" ) -- add entry using its base name "file1.txt" zip_add( zip, "path/to/file1.txt" ) -- add entry using specific name "path/file2.txt" zip_add( zip, "path/to/file2.txt", "path/file2.txt" ) -- add entry using its base name and no compression zip_add( zip, "path/to/file3.txt",, ZIP_NO_COMPRESSION ) -- finalize (write directory) and close the zip file zip_close( zip )
Inspecting zip files
include std/zip.e -- open zip file for reading atom zip = zip_open( "test.zip", "r" ) -- find a specific file (returns ordinal number or -1 if not found) ? zip_find( zip, "file3.txt" ) -- list all entries, with optional wildcard (same input/output as dir()) sequence files = zip_dir( zip ) for i = 1 to length( files ) do printf( 1, "name: \"%s\", size: %d bytes\n", {files[i][D_NAME],files[i][D_SIZE]} ) end for -- close the zip file zip_close( zip )
Extracting zip files
include std/zip.e -- open zip file for reading atom zip = zip_open( "test.zip", "r" ) -- extract the entry (and its path) to the current directory zip_extract( zip, "path/file2.txt" ) -- extract the entry to a specific destination (to ignore path) zip_extract( zip, "path/file2.txt", "file.txt" ) -- close the zip file zip_close( zip )
Thoughts, comments, opinions?
-Greg

