Skip to content
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

Added ArchiveComment Property to ZipArchive #341

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/SharpCompress/Archives/Zip/ZipArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class ZipArchive : AbstractWritableArchive<ZipArchiveEntry, ZipVolume>
/// </summary>
public CompressionLevel DeflateCompressionLevel { get; set; }

public string ArchiveComment { get; private set; }

#if !NO_FILE

/// <summary>
Expand Down Expand Up @@ -154,6 +156,8 @@ protected override IEnumerable<ZipArchiveEntry> LoadEntries(IEnumerable<ZipVolum
{
case ZipHeaderType.DirectoryEntry:
{
byte[] bytes = StringToByteArray((h as DirectoryEntryHeader).Comment);
ArchiveComment = ReaderOptions.ArchiveEncoding.Decode(bytes);
yield return new ZipArchiveEntry(this,
new SeekableZipFilePart(headerFactory,
h as DirectoryEntryHeader,
Expand Down Expand Up @@ -210,5 +214,14 @@ protected override IReader CreateReaderForSolidExtraction()
stream.Position = 0;
return ZipReader.Open(stream, ReaderOptions);
}

private byte[] StringToByteArray(string hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,6 @@ internal override void Read(BinaryReader reader)

public ushort DiskNumberStart { get; set; }

public string Comment { get; private set; }
public string Comment { get; set; }
}
}
13 changes: 13 additions & 0 deletions src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,25 @@ internal IEnumerable<DirectoryEntryHeader> ReadSeekableHeader(Stream stream)
yield break;
}

if (entry.Comment.Length != 0)
{
directoryEntryHeader.Comment = ByteArrayToString(entry.Comment);
}

//entry could be zero bytes so we need to know that.
directoryEntryHeader.HasData = directoryEntryHeader.CompressedSize != 0;
yield return directoryEntryHeader;
}
}

internal string ByteArrayToString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}

private static void SeekBackToHeader(Stream stream, BinaryReader reader, uint headerSignature)
{
long offset = 0;
Expand Down
23 changes: 22 additions & 1 deletion tests/SharpCompress.Test/Zip/ZipArchiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

namespace SharpCompress.Test.Zip
{
public class ZipArchiveTests : ArchiveTests
using Microsoft.VisualStudio.TestPlatform.ObjectModel;

using SharpCompress.Writers.Zip;

public class ZipArchiveTests : ArchiveTests
{
public ZipArchiveTests()
{
Expand Down Expand Up @@ -461,5 +465,22 @@ public void TestSharpCompressWithEmptyStream()
}
}
}

[Fact]
public void ReadArchiveComment()
{
string expectedComment = "HelloWorld";
using (var streamArchive = ZipArchive.Create())
{
var zipStream = new MemoryStream();
streamArchive.AddEntry("Dummy", new MemoryStream());
streamArchive.SaveTo(zipStream, new ZipWriterOptions(CompressionType.Deflate) { ArchiveComment = expectedComment });
using (var openedArchive = ZipArchive.Open(zipStream))
{
var entries = openedArchive.Entries.ToList();
Assert.Equal(expectedComment, openedArchive.ArchiveComment);
}
}
}
}
}