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

Add Folders to ZipArchive #342

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
18 changes: 15 additions & 3 deletions src/SharpCompress/Archives/Zip/ZipArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,18 @@ public void SaveTo(Stream stream)
{
using (var writer = new ZipWriter(stream, new ZipWriterOptions(options)))
{
foreach (var entry in oldEntries.Concat(newEntries)
.Where(x => !x.IsDirectory))
foreach (var entry in oldEntries.Concat(newEntries))
{
using (var entryStream = entry.OpenEntryStream())
if (entry.IsDirectory)
{
//TODO What do to here?
}
else
{
using (var entryStream = entry.OpenEntryStream())
{
writer.Write(entry.Key, entryStream, entry.LastModifiedTime);
}
}
}
}
Expand All @@ -210,5 +216,11 @@ protected override IReader CreateReaderForSolidExtraction()
stream.Position = 0;
return ZipReader.Open(stream, ReaderOptions);
}

public void AddDirectory(string combine)
{
combine += "/";
var entry = AddEntry(combine, new MemoryStream());
}
}
}
14 changes: 13 additions & 1 deletion src/SharpCompress/Archives/Zip/ZipWritableArchiveEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,19 @@ internal class ZipWritableArchiveEntry : ZipArchiveEntry, IWritableArchiveEntry

public override bool IsEncrypted => false;

public override bool IsDirectory => false;
public override bool IsDirectory
{
get
{
if (Key.EndsWith("/"))
{
return true;
}

//.NET Framework 4.5 : System.IO.Compression::CreateFromDirectory() probably writes backslashes to headers
return CompressedSize == 0 && Key.EndsWith("\\");
}
}

public override bool IsSplit => false;

Expand Down
24 changes: 24 additions & 0 deletions tests/SharpCompress.Test/Zip/AddFolderTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace SharpCompress.Test.Zip
{
using System.IO;

using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
using SharpCompress.Common;
using SharpCompress.Writers.Zip;

using Xunit;

public class AddFolderTest
{
[Fact]
public void When_subject_should_expectation()
{
using (var zip = ZipArchive.Create())
{
zip.AddDirectory(Path.Combine("Hallo", "Welt"));
zip.SaveTo(@"D:\Hallo.zip", new ZipWriterOptions(CompressionType.Deflate) { ArchiveComment = "HalloWelt" });
}
}
}
}
18 changes: 18 additions & 0 deletions tests/SharpCompress.Test/Zip/ZipArchiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,5 +461,23 @@ public void TestSharpCompressWithEmptyStream()
}
}
}

[Fact]
public void When_adding_empty_folder_should_only_contain_folders()
{
var stream = new MemoryStream();
string folderName = "EmptyFolder";
using (var zipArchive = ZipArchive.Create())
{
zipArchive.AddDirectory(folderName);
zipArchive.SaveTo(stream);
}

using (var zipArchive = ZipArchive.Open(stream))
{
Assert.True(zipArchive.Entries.SingleOrDefault(me => me.Key.Contains("folderName")) != null);
Assert.True(zipArchive.Entries.All(me => me.IsDirectory));
}
}
}
}