-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
zip: Entry.name(); Entry.extract_to() accepting user-provided writers #23991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Disclaimer: I'm only a contributor, not a Zig core team member, so take my suggestions with a grain of salt.
lib/std/zip.zig
Outdated
var dir = try extractor.openDir(filename[0 .. filename.len - 1]); | ||
dir.close(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The purpose of this openDir
function really isn't to open a directory, but to create directories to form that path, which is evident by the filesystem implementation of openDir below and the original code that was here before these changes.
There's also not much reason to have a close
function; any extractor implementation could just call it before returning from it's openDir (or more accurately makePath) function if it was necessary. The filesystem implementation implements it as a no-op anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with this.
lib/std/zip.zig
Outdated
var file = try extractor.openFile(filename); | ||
defer file.close(); | ||
const crc = try decompress( | ||
self.compression_method, | ||
self.uncompressed_size, | ||
limited_reader.reader(), | ||
out_file.writer(), | ||
file.writer(), | ||
); | ||
if (limited_reader.bytes_left != 0) | ||
return error.ZipDecompressTruncated; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a thought, but I do wonder if this could be factored out to remove the need for this extractor interface altogether, since all that's really needed here is a writer to decompress the data to.
For example, maybe have extract_to return a struct containing the filename/path, crc, and a "file or directory" enum, then this code that actually does the decompression and the openDir code above that creates the directory could be moved into extract, and the extractor interface can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decompression depends on the (generalized concept of) opened file and opening the file depends on the filename. Then decompression may have to be moved from extract_to
to its caller, too. That's too much for callers I'm afraid.
lib/std/zip.zig
Outdated
fn openFile(self: @This(), name: []u8) !std.fs.File { | ||
if (std.fs.path.dirname(name)) |dirname| { | ||
var parent_dir = try self.dest.makeOpenPath(dirname, .{}); | ||
defer parent_dir.close(); | ||
|
||
const basename = std.fs.path.basename(name); | ||
return try parent_dir.createFile(basename, .{ .exclusive = true }); | ||
} | ||
return try self.dest.createFile(name, .{ .exclusive = true }); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this function creates a file and ensures it's parent directory exists, and doesn't open a file in the usual sense, it should probably be renamed as such. Of course if the extractor interface is removed (see comment above), this wouldn't be relevant.
After using
|
std.zip
users sometimes wish to extract content in memory rather than to the file system. This change adds a newextract_to
as a more flexible alternative to the existingextract
.This partly addresses #21922.
I am a first-time contributor to zig. Comments and suggestions are greatly appreciated.