Skip to content
Permalink
Browse files

[SharpCompress] Add support for custom entry text encoding in Zip wri…

…ter.
  • Loading branch information...
tritao committed Jun 11, 2015
1 parent 4da1e26 commit 12e2b1d116b8cd1420426928d5ed2881b672c724
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using SharpCompress.Common;

namespace SharpCompress.Archive
@@ -105,11 +106,11 @@ private bool DoesKeyMatchExisting(string key)
return false;
}

public void SaveTo(Stream stream, CompressionInfo compressionType)
public void SaveTo(Stream stream, CompressionInfo compressionType, Encoding encoding = null)
{
//reset streams of new entries
newEntries.Cast<IWritableArchiveEntry>().ForEach(x => x.Stream.Seek(0, SeekOrigin.Begin));
SaveTo(stream, compressionType, OldEntries, newEntries);
SaveTo(stream, compressionType, encoding ?? ArchiveEncoding.Default, OldEntries, newEntries);
}

protected TEntry CreateEntry(string key, Stream source, long size, DateTime? modified,
@@ -125,7 +126,7 @@ public void SaveTo(Stream stream, CompressionInfo compressionType)
protected abstract TEntry CreateEntryInternal(string key, Stream source, long size, DateTime? modified,
bool closeStream);

protected abstract void SaveTo(Stream stream, CompressionInfo compressionType,
protected abstract void SaveTo(Stream stream, CompressionInfo compressionType, Encoding encoding,
IEnumerable<TEntry> oldEntries, IEnumerable<TEntry> newEntries);

public override void Dispose()
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using SharpCompress.Common;
using SharpCompress.Common.Zip;
using SharpCompress.Common.Zip.Headers;
@@ -205,13 +206,13 @@ protected override IEnumerable<ZipArchiveEntry> LoadEntries(IEnumerable<ZipVolum
}
}
}
}
}

protected override void SaveTo(Stream stream, CompressionInfo compressionInfo,
protected override void SaveTo(Stream stream, CompressionInfo compressionInfo, Encoding encoding,
IEnumerable<ZipArchiveEntry> oldEntries,
IEnumerable<ZipArchiveEntry> newEntries)
{
using (var writer = new ZipWriter(stream, compressionInfo, string.Empty))
using (var writer = new ZipWriter(stream, compressionInfo, string.Empty, encoding))
{
foreach (var entry in oldEntries.Concat(newEntries)
.Where(x => !x.IsDirectory))
@@ -30,16 +30,18 @@ internal class ZipWriter : AbstractWriter

private readonly List<ZipCentralDirectoryEntry> entries = new List<ZipCentralDirectoryEntry>();
private readonly string zipComment;
private readonly Encoding encoding;
private long streamPosition;

#if PPMd
private readonly PpmdProperties ppmdProperties; // Caching properties to speed up PPMd.
#endif

public ZipWriter(Stream destination, CompressionInfo compressionInfo, string zipComment)
public ZipWriter(Stream destination, CompressionInfo compressionInfo, string zipComment, Encoding encoding = null)
: base(ArchiveType.Zip)
{
this.zipComment = zipComment ?? string.Empty;
this.encoding = encoding ?? ArchiveEncoding.Default;

switch (compressionInfo.Type)
{
@@ -137,11 +139,11 @@ private string NormalizeFilename(string filename)

private int WriteHeader(string filename, DateTime? modificationTime)
{
byte[] encodedFilename = Encoding.UTF8.GetBytes(filename);
byte[] encodedFilename = encoding.GetBytes(filename);

OutputStream.Write(BitConverter.GetBytes(ZipHeaderFactory.ENTRY_HEADER_BYTES), 0, 4);
OutputStream.Write(new byte[] {63, 0}, 0, 2); //version
HeaderFlags flags = HeaderFlags.UTF8;
HeaderFlags flags = encoding == Encoding.UTF8 ? HeaderFlags.UTF8 : (HeaderFlags)0;
if (!OutputStream.CanSeek)
{
flags |= HeaderFlags.UsePostDataDescriptor;
@@ -172,7 +174,7 @@ private void WriteFooter(uint crc, uint compressed, uint uncompressed)

private void WriteEndRecord(uint size)
{
byte[] encodedComment = Encoding.UTF8.GetBytes(zipComment);
byte[] encodedComment = encoding.GetBytes(zipComment);

OutputStream.Write(new byte[] {80, 75, 5, 6, 0, 0, 0, 0}, 0, 8);
OutputStream.Write(BitConverter.GetBytes((ushort) entries.Count), 0, 2);

0 comments on commit 12e2b1d

Please sign in to comment.
You can’t perform that action at this time.