Skip to content

Commit 007f703

Browse files
authored
Merge pull request #75 from guzba/ryan
0.10.12
2 parents 1328b01 + 6faa6dd commit 007f703

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

examples/ziparchive_create.nim

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import zippy/ziparchives, std/tables
2+
3+
# This example shows how to easily create an in-memory zip archive that can be
4+
# written to disk or uploaded to a server, etc.
5+
6+
# First, add the entries you want in the zip archive.
7+
# The key is the path (must be relative) and the value is the content bytes.
8+
var entries: Table[string, string]
9+
entries["file.txt"] = "Hello, Zip!"
10+
entries["data/blob.json"] = "{}"
11+
12+
# Creates a zip archive containing the compressed entries.
13+
let archive = createZipArchive(entries)
14+
15+
# This zip archive can be written to disk:
16+
writeFile("tmp.zip", archive)

src/zippy/ziparchives.nim

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,10 @@ proc extractAll*(
453453
reader.close()
454454

455455
when (NimMajor, NimMinor, NimPatch) >= (1, 6, 0):
456-
proc createZipArchive*(
457-
entries: sink OrderedTable[string, string]
458-
): string {.raises: [ZippyError].} =
456+
# For some reason `sink Table | OrderedTable` does not work, so work around:
457+
template createZipArchiveImpl(
458+
entries: var Table[string, string] | var OrderedTable[string, string]
459+
) =
459460

460461
proc add16(dst: var string, v: int16 | uint16) =
461462
dst.setLen(dst.len + 2)
@@ -618,3 +619,13 @@ when (NimMajor, NimMinor, NimPatch) >= (1, 6, 0):
618619
result.add32(uint32.high) # Size of central directory (bytes) (or 0xffffffff for ZIP64)
619620
result.add32(uint32.high) # Offset of start of central directory, relative to start of archive (or 0xffffffff for ZIP64)
620621
result.add16(0)
622+
623+
proc createZipArchive*(
624+
entries: sink Table[string, string]
625+
): string {.raises: [ZippyError].} =
626+
createZipArchiveImpl(entries)
627+
628+
proc createZipArchive*(
629+
entries: sink OrderedTable[string, string]
630+
): string {.raises: [ZippyError].} =
631+
createZipArchiveImpl(entries)

zippy.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version = "0.10.11"
1+
version = "0.10.12"
22
author = "Ryan Oldenburg"
33
description = "Pure Nim implementation of deflate, zlib, gzip and zip."
44
license = "MIT"

0 commit comments

Comments
 (0)