From 09ebb4089676fe029dd09e6a83dcf5ac7126ad0e Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Tue, 9 Apr 2024 22:32:48 -0500 Subject: [PATCH] mac --- src/zippy/ziparchives.nim | 30 ++++++++++++++---------------- tests/test_ziparchives_write.nim | 17 ++++++++++++++--- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/zippy/ziparchives.nim b/src/zippy/ziparchives.nim index da6500c..1add653 100644 --- a/src/zippy/ziparchives.nim +++ b/src/zippy/ziparchives.nim @@ -6,13 +6,11 @@ export common, ziparchives_v1 const fileHeaderLen = 30 fileHeaderSignature = 0x04034b50.uint32 - dataDescriptorSignature = 0x08074b50.uint32 centralDirectoryFileHeaderSignature = 0x02014b50.uint32 endOfCentralDirectorySignature = 0x06054b50.uint32 zip64EndOfCentralDirectorySignature = 0x06064b50.uint32 zip64EndOfCentralDirectoryLocatorSignature = 0x07064b50.uint32 zip64ExtraFieldId = 1.uint16 - zip64FileHeaderExtraLen = 28 type ZipArchiveRecordKind = enum @@ -533,18 +531,23 @@ proc createZipArchive*( result.add32(fileHeaderSignature) result.add16(45) # Min version to extract - result.add16((1.uint16 shl 3) or (1.uint16 shl 11)) # General purpose flags + result.add16(1.uint16 shl 11) # General purpose flags result.add16(compressionMethod) result.add16(lastModifiedTime) result.add16(lastModifiedDate) - result.add32(0) # CRC-32 of uncompressed data (in trailing data descriptor) + result.add32(uncompressedCrc32) # CRC-32 of uncompressed data result.add32(uint32.high) # Compressed size (or 0xffffffff for ZIP64) result.add32(uint32.high) # Uncompressed size (or 0xffffffff for ZIP64) result.add16(cast[uint16](path.len)) # File name length - result.add16(0) # Extra field length + result.add16(20) # Extra field length result.add(path) + result.add16(zip64ExtraFieldId) + result.add16(16) + result.add64(contents.len) + result.add64(compressed.len) + # result.add(compressed) if compressed != "": result.setLen(result.len + compressed.len) @@ -554,18 +557,13 @@ proc createZipArchive*( compressed.len ) - result.add32(dataDescriptorSignature) - result.add32(uncompressedCrc32) - result.add64(compressed.len) - result.add64(contents.len) - let centralDirectoryStart = result.len for i in 0 ..< records.len: let entry = records[i][1] result.add32(centralDirectoryFileHeaderSignature) - result.add16(20) # Version made by - result.add16(20) # Min version to extract + result.add16(45) # Version made by + result.add16(45) # Min version to extract result.add16(1.uint16 shl 11) # General purpose flags result.add16(entry.compressionMethod) result.add16(lastModifiedTime) @@ -608,10 +606,10 @@ proc createZipArchive*( result.add32(1) result.add32(endOfCentralDirectorySignature) - result.add32(0) # Number of this disk - result.add32(0) # Disk where central directory starts - result.add32(uint32.high) # Number of central directory records on this disk (or 0xffff for ZIP64) - result.add32(uint32.high) # Total number of central directory records (or 0xffff for ZIP64) + result.add16(0) # Number of this disk + result.add16(0) # Disk where central directory starts + result.add16(uint16.high) # Number of central directory records on this disk (or 0xffff for ZIP64) + result.add16(uint16.high) # Total number of central directory records (or 0xffff for ZIP64) 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) diff --git a/tests/test_ziparchives_write.nim b/tests/test_ziparchives_write.nim index a10ca4a..3fc7e7f 100644 --- a/tests/test_ziparchives_write.nim +++ b/tests/test_ziparchives_write.nim @@ -1,7 +1,7 @@ import std/os, zippy/ziparchives, std/tables, std/strutils # var entries: OrderedTable[string, string] -# entries["a/b/tmp.txt"] = "a text file's contents here" +# entries["README.txt"] = "Hello, World!" # let archive = createZipArchive(entries) # echo archive.len @@ -15,8 +15,8 @@ import std/os, zippy/ziparchives, std/tables, std/strutils # echo path # # # Extract a file from the archive. -# let contents = reader.extractFile("a/b/tmp.txt") -# echo contents.len +# # let contents = reader.extractFile("tmp.txt") +# # echo contents.len # finally: # # Remember to close the reader when done. # reader.close() @@ -29,3 +29,14 @@ import std/os, zippy/ziparchives, std/tables, std/strutils # continue # entries[path] = readFile(path) # writeFile("tmp.zip", createZipArchive(entries)) + +# let reader = openZipArchive("tmp.zip") + +# try: +# # Iterate over the paths in the zip archive. +# for path in reader.walkFiles: +# echo path + +# finally: +# # Remember to close the reader when done. +# reader.close()