From 6faa6dd7206ea8d8085da63d5220489916948394 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Sun, 14 Apr 2024 23:59:05 -0500 Subject: [PATCH] 0.10.12 --- examples/ziparchive_create.nim | 16 ++++++++++++++++ src/zippy/ziparchives.nim | 17 ++++++++++++++--- zippy.nimble | 2 +- 3 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 examples/ziparchive_create.nim diff --git a/examples/ziparchive_create.nim b/examples/ziparchive_create.nim new file mode 100644 index 0000000..541cae2 --- /dev/null +++ b/examples/ziparchive_create.nim @@ -0,0 +1,16 @@ +import zippy/ziparchives, std/tables + +# This example shows how to easily create an in-memory zip archive that can be +# written to disk or uploaded to a server, etc. + +# First, add the entries you want in the zip archive. +# The key is the path (must be relative) and the value is the content bytes. +var entries: Table[string, string] +entries["file.txt"] = "Hello, Zip!" +entries["data/blob.json"] = "{}" + +# Creates a zip archive containing the compressed entries. +let archive = createZipArchive(entries) + +# This zip archive can be written to disk: +writeFile("tmp.zip", archive) diff --git a/src/zippy/ziparchives.nim b/src/zippy/ziparchives.nim index 7299fe3..00afbff 100644 --- a/src/zippy/ziparchives.nim +++ b/src/zippy/ziparchives.nim @@ -453,9 +453,10 @@ proc extractAll*( reader.close() when (NimMajor, NimMinor, NimPatch) >= (1, 6, 0): - proc createZipArchive*( - entries: sink OrderedTable[string, string] - ): string {.raises: [ZippyError].} = + # For some reason `sink Table | OrderedTable` does not work, so work around: + template createZipArchiveImpl( + entries: var Table[string, string] | var OrderedTable[string, string] + ) = proc add16(dst: var string, v: int16 | uint16) = dst.setLen(dst.len + 2) @@ -618,3 +619,13 @@ when (NimMajor, NimMinor, NimPatch) >= (1, 6, 0): result.add32(uint32.high) # Size of central directory (bytes) (or 0xffffffff for ZIP64) result.add32(uint32.high) # Offset of start of central directory, relative to start of archive (or 0xffffffff for ZIP64) result.add16(0) + + proc createZipArchive*( + entries: sink Table[string, string] + ): string {.raises: [ZippyError].} = + createZipArchiveImpl(entries) + + proc createZipArchive*( + entries: sink OrderedTable[string, string] + ): string {.raises: [ZippyError].} = + createZipArchiveImpl(entries) diff --git a/zippy.nimble b/zippy.nimble index 9842fea..ea6f4a0 100644 --- a/zippy.nimble +++ b/zippy.nimble @@ -1,4 +1,4 @@ -version = "0.10.11" +version = "0.10.12" author = "Ryan Oldenburg" description = "Pure Nim implementation of deflate, zlib, gzip and zip." license = "MIT"