Skip to content

Commit

Permalink
Merge pull request #75 from guzba/ryan
Browse files Browse the repository at this point in the history
0.10.12
  • Loading branch information
guzba authored Apr 15, 2024
2 parents 1328b01 + 6faa6dd commit 007f703
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
16 changes: 16 additions & 0 deletions examples/ziparchive_create.nim
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 14 additions & 3 deletions src/zippy/ziparchives.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion zippy.nimble
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit 007f703

Please sign in to comment.