-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Add the ArchiveComment property for ZipArchive class #1547
Comments
related to https://github.com/dotnet/corefx/issues/14853 and (to a lesser extent) https://github.com/dotnet/corefx/issues/12066 and https://github.com/dotnet/corefx/issues/10223 There are many fields in the ZipArchive header that I would like to see exposed in some manner eventually. I'm a bit hesitant to start exposing them one-by-one as we may end up with a messy API that we wish we could change but can't because of compat. I'd rather we look at the entire Zip header and decide what to expose - and how - all at once. https://github.com/dotnet/corefx/issues/14853 is one possible way of doing this. |
Python exposes the zip archive comment in their public APIs, for both the ZipFile (the actual archive) and the ZipInfo (an individual archive member). Java offers only a get method for the ZipFile comment, but has a get and set methods for the ZipEntry. WinZip and WinRAR have the ability to show it too. The 7-zip, Windows Explorer and corefx do not show the zip archive comment, although Windows Explorer allows hovering on a zip file and show a snippet of that comment, but not all. |
We are already collecting the archive comment: runtime/src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchive.cs Line 30 in 57bfe47
And the file comment: runtime/src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchiveEntry.cs Line 43 in 57bfe47
So making these fields public is just a matter of ensuring the setters are properly delimited according to the spec. The API proposal may look like this: namespace System.IO.Compression
{
public partial class ZipArchive : IDisposable
{
public ZipArchive(Stream stream);
public ZipArchive(Stream stream, ZipArchiveMode mode);
public ZipArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen);
public ZipArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen, Encoding? entryNameEncoding);
+ public string? Comment { get; set; }
public ReadOnlyCollection<ZipArchiveEntry> Entries { get; }
public ZipArchiveMode Mode { get; }
public ZipArchiveEntry CreateEntry(string entryName);
public ZipArchiveEntry CreateEntry(string entryName, CompressionLevel compressionLevel);
public void Dispose();
protected virtual void Dispose(bool disposing);
public ZipArchiveEntry? GetEntry(string entryName);
}
public partial class ZipArchiveEntry
{
internal ZipArchiveEntry() { }
public ZipArchive Archive { get ; }
+ public string? Comment { get; set; }
public long CompressedLength { get ; }
public uint Crc32 { get; }
public int ExternalAttributes { get; set; }
public string FullName { get; }
public DateTimeOffset LastWriteTime { get; set; }
public long Length { get; }
public string Name { get; }
public void Delete();
public Stream Open();
public override string ToString();
}
} Questions
Additional detailsThe ZIP file format spec indicates that:
Archive comment restrictionsArchive comment spec definition
Comment encoding
And also:
Max size of comment
File comment restrictionsFile comment spec definition
Non-encryption of archive comment
Max size of comment
NoteThere's mention of a |
Since the spec says that there's always a comment (but it can be zero length) we don't think the properties should be nullable (null-returning). The setters can certainly accept null to normalize to whatever the default we have historically done. (string.Empty / "Built with .NET", whatever it is we do...) If you don't want to do null-normalization, then remove the namespace System.IO.Compression
{
public partial class ZipArchive : IDisposable
{
public ZipArchive(Stream stream);
public ZipArchive(Stream stream, ZipArchiveMode mode);
public ZipArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen);
public ZipArchive(Stream stream, ZipArchiveMode mode, bool leaveOpen, Encoding? entryNameEncoding);
+ [AllowNull]
+ public string Comment { get; set; }
public ReadOnlyCollection<ZipArchiveEntry> Entries { get; }
public ZipArchiveMode Mode { get; }
public ZipArchiveEntry CreateEntry(string entryName);
public ZipArchiveEntry CreateEntry(string entryName, CompressionLevel compressionLevel);
public void Dispose();
protected virtual void Dispose(bool disposing);
public ZipArchiveEntry? GetEntry(string entryName);
}
public partial class ZipArchiveEntry
{
internal ZipArchiveEntry() { }
public ZipArchive Archive { get ; }
+ [AllowNull]
+ public string Comment { get; set; }
public long CompressedLength { get ; }
public uint Crc32 { get; }
public int ExternalAttributes { get; set; }
public string FullName { get; }
public DateTimeOffset LastWriteTime { get; set; }
public long Length { get; }
public string Name { get; }
public void Delete();
public Stream Open();
public override string ToString();
}
} |
Edit by carlossanlop: API Proposal here
When we make a zip package, the comment is very important function that we use it very often. The corefx has already implemented it, see the definition
_archiveComment
field at ZipArchive.cs#L32, the reader at ZipArchive.cs#L561 and the writter at ZipArchive.cs#L693.We can inbreak the
ZipArchive
instance by reflect and modify the_archiveComment
field, then saved zip file is fine.Proposed API
Just make private field
_archiveComment
as public propertyArchiveComment
, or other advises.The text was updated successfully, but these errors were encountered: