Skip to content
Closed
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
1 change: 1 addition & 0 deletions src/ICSharpCode.SharpZipLib/Zip/FastZip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ public void CreateZip(Stream outputStream, string sourceDirectory, bool recurse,
using (outputStream_ = new ZipOutputStream(outputStream))
{
outputStream_.SetLevel((int)CompressionLevel);
outputStream_.TransformEntryNames = false; // all required transforms handled by us

if (password_ != null)
{
Expand Down
49 changes: 47 additions & 2 deletions src/ICSharpCode.SharpZipLib/Zip/ZipOutputStream.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ICSharpCode.SharpZipLib.Checksum;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
using System;
Expand Down Expand Up @@ -146,6 +147,11 @@ public UseZip64 UseZip64
set { useZip64_ = value; }
}

/// <summary>
/// Controls whether to transform the names of entries added by <see cref="PutNextEntry(ZipEntry)"/> into Zip format, or to leave them as they are.
/// </summary>
public bool TransformEntryNames { get; set; } = true;

/// <summary>
/// Write an unsigned short in little endian byte order.
/// </summary>
Expand Down Expand Up @@ -182,6 +188,45 @@ private void WriteLeLong(long value)
}
}

// Transform entry names into zip format by fixing up separators and removing path roots.
private string TransformEntryName(ZipEntry entry)
{
string transformedName = entry.Name;

if (this.TransformEntryNames)
{
transformedName = transformedName.Replace(@"\", "/");
transformedName = WindowsPathUtils.DropPathRoot(transformedName);

// Drop any leading and trailing slashes.
transformedName = transformedName.Trim('/');

// Convert consecutive // characters to /
int index = transformedName.IndexOf("//", StringComparison.Ordinal);
while (index >= 0)
{
transformedName = transformedName.Remove(index, 1);
index = transformedName.IndexOf("//", StringComparison.Ordinal);
}

// Directory entries need just a single / on the end
if (entry.IsDirectory)
{
transformedName += "/";
}
}

return transformedName;
}

// Convert an entry name to a byte array, and apply any transforms
private byte[] TransformEntryNameToArray(ZipEntry entry)
{
string transformedName = TransformEntryName(entry);
byte[] name = ZipStrings.ConvertToArray(entry.Flags, transformedName);
return name;
}

/// <summary>
/// Starts a new Zip entry. It automatically closes the previous
/// entry if present.
Expand Down Expand Up @@ -367,7 +412,7 @@ public void PutNextEntry(ZipEntry entry)
}
}

byte[] name = ZipStrings.ConvertToArray(entry.Flags, entry.Name);
byte[] name = TransformEntryNameToArray(entry);

if (name.Length > 0xFFFF)
{
Expand Down Expand Up @@ -787,7 +832,7 @@ public override void Finish()
WriteLeInt((int)entry.Size);
}

byte[] name = ZipStrings.ConvertToArray(entry.Flags, entry.Name);
byte[] name = TransformEntryNameToArray(entry);

if (name.Length > 0xffff)
{
Expand Down