summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-09-15 22:07:26 +0100
committerMartin Ashby <martin@ashbysoft.com>2023-09-15 22:07:26 +0100
commit40847ee327129a7466792b7606449bd91968c42f (patch)
treea13de2171ab04ddbf478cd9d58c94803a6d65873
parente3f21b6c3253da86dec6cb231581cbbbd4b4a2d7 (diff)
downloadzip-zig-40847ee327129a7466792b7606449bd91968c42f.tar.gz
zip-zig-40847ee327129a7466792b7606449bd91968c42f.tar.bz2
zip-zig-40847ee327129a7466792b7606449bd91968c42f.tar.xz
zip-zig-40847ee327129a7466792b7606449bd91968c42f.zip
Add a test for deflate and comment
Add a method for getting a comment
-rw-r--r--src/deflate.zipbin0 -> 449 bytes
-rw-r--r--src/foo.txt2
-rw-r--r--src/main.zig20
3 files changed, 19 insertions, 3 deletions
diff --git a/src/deflate.zip b/src/deflate.zip
new file mode 100644
index 0000000..1a6826e
--- /dev/null
+++ b/src/deflate.zip
Binary files differ
diff --git a/src/foo.txt b/src/foo.txt
index 458d9cb..decec2d 100644
--- a/src/foo.txt
+++ b/src/foo.txt
@@ -1 +1 @@
-The quick brown fox jumped over the lazy dog
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. \ No newline at end of file
diff --git a/src/main.zig b/src/main.zig
index 20c939a..417f10b 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -111,6 +111,9 @@ const ZipFile = struct {
fn file_name(self: ZipFile, index: u16) []const u8 {
return self.central_directory_headers[index].file_name;
}
+ fn file_comment(self: ZipFile, index: u16) []const u8 {
+ return self.central_directory_headers[index].file_comment;
+ }
fn extract(self: ZipFile, index: u16, stream_or_file_in: anytype, stream_or_file_out: anytype) !void {
const cdh = self.central_directory_headers[index];
try stream_or_file_in.seekTo(cdh.relative_offset_of_local_header);
@@ -170,7 +173,7 @@ const CentralDirectoryHeader = struct {
version_made_by: u16,
version_needed_to_extract: u16,
general_purpose_bit_flag: u16,
- compression_method: u16,
+ compression_method: ZipFile.CompressionMethod,
last_mod_file_time: u16,
last_mod_file_date: u16,
crc32: u32,
@@ -330,7 +333,7 @@ test "foo2" {
try std.testing.expectEqualStrings(zf.file_name(1), "foo.txt");
}
-test "extract" {
+test "extract stored" {
const test_zip = @embedFile("hello.zip");
var fbs = std.io.fixedBufferStream(test_zip);
var zf = try ZipFile.from(std.testing.allocator, &fbs);
@@ -343,3 +346,16 @@ test "extract" {
try zf.extract(1, &fbs, &fbs_out);
try std.testing.expectEqualStrings("hi there\n", fbs_out.getWritten());
}
+
+test "extract deflate" {
+ const test_zip = @embedFile("deflate.zip");
+ var fbs = std.io.fixedBufferStream(test_zip);
+ var zf = try ZipFile.from(std.testing.allocator, &fbs);
+ defer zf.deinit();
+ var out = [_]u8{0} ** 1024;
+ var fbs_out = std.io.fixedBufferStream(&out);
+ try std.testing.expectEqualStrings("Here is a comment :)", zf.file_comment(0));
+ try std.testing.expectEqual(ZipFile.CompressionMethod.deflate, zf.central_directory_headers[0].compression_method);
+ try zf.extract(0, &fbs, &fbs_out);
+ try std.testing.expectEqualStrings(@embedFile("foo.txt"), fbs_out.getWritten());
+}