Compare commits

..

2 Commits

Author SHA1 Message Date
Adam Hathcock
703e4bd49e add pkware zip 2017-08-17 08:47:46 +01:00
Adam Hathcock
30d4251332 Trying to make this work. Conflicts with reading on post descriptor header style 2017-08-08 10:16:27 +01:00
20 changed files with 200 additions and 329 deletions

View File

@@ -208,7 +208,7 @@ namespace SharpCompress.Archives.Zip
{
var stream = Volumes.Single().Stream;
stream.Position = 0;
return ZipReader.Open(stream, ReaderOptions);
return ZipReader.Open(stream);
}
}
}

View File

@@ -182,7 +182,7 @@ namespace SharpCompress.Common.SevenZip
private DateTime? TranslateTime(long? time)
{
if (time.HasValue && time.Value >= 0 && time.Value <= 2650467743999999999) //maximum Windows file time 31.12.9999
if (time.HasValue)
{
return TranslateTime(time.Value);
}
@@ -1588,4 +1588,4 @@ namespace SharpCompress.Common.SevenZip
#endregion
}
}
}

View File

@@ -0,0 +1,8 @@
namespace SharpCompress.Common.Zip
{
internal enum CryptoMode
{
Encrypt,
Decrypt
}
}

View File

@@ -3,12 +3,6 @@ using System.IO;
namespace SharpCompress.Common.Zip
{
internal enum CryptoMode
{
Encrypt,
Decrypt
}
internal class PkwareTraditionalCryptoStream : Stream
{
private readonly PkwareTraditionalEncryptionData encryptor;

View File

@@ -33,10 +33,6 @@ namespace SharpCompress.Common.Zip
byte[] plainTextHeader = encryptor.Decrypt(encryptionHeader, encryptionHeader.Length);
if (plainTextHeader[11] != (byte)((header.Crc >> 24) & 0xff))
{
if (!FlagUtility.HasFlag(header.Flags, HeaderFlags.UsePostDataDescriptor))
{
throw new CryptographicException("The password did not match.");
}
if (plainTextHeader[11] != (byte)((header.LastModifiedTime >> 8) & 0xff))
{
throw new CryptographicException("The password did not match.");
@@ -108,5 +104,10 @@ namespace SharpCompress.Common.Zip
_Keys[1] = _Keys[1] * 0x08088405 + 1;
_Keys[2] = (UInt32)crc32.ComputeCrc32((int)_Keys[2], (byte)(_Keys[1] >> 24));
}
public static PkwareTraditionalEncryptionData ForWrite(string password, ArchiveEncoding archiveEncoding)
{
return new PkwareTraditionalEncryptionData(password, archiveEncoding);
}
}
}

View File

@@ -128,11 +128,6 @@ namespace SharpCompress.Common.Zip
{
bool isFileEncrypted = FlagUtility.HasFlag(Header.Flags, HeaderFlags.Encrypted);
if (Header.CompressedSize == 0 && isFileEncrypted)
{
throw new NotSupportedException("Cannot encrypt file with unknown size at start.");
}
if ((Header.CompressedSize == 0
&& FlagUtility.HasFlag(Header.Flags, HeaderFlags.UsePostDataDescriptor))
|| Header.IsZip64)

View File

@@ -418,7 +418,7 @@ namespace SharpCompress.Compressors.Deflate
internal sealed class Adler
{
// largest prime smaller than 65536
private static readonly uint BASE = 65521U;
private static readonly int BASE = 65521;
// NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
private static readonly int NMAX = 5552;
@@ -430,8 +430,8 @@ namespace SharpCompress.Compressors.Deflate
return 1;
}
uint s1 = adler & 0xffffU;
uint s2 = (adler >> 16) & 0xffffU;
int s1 = (int)(adler & 0xffff);
int s2 = (int)((adler >> 16) & 0xffff);
while (len > 0)
{
@@ -486,7 +486,7 @@ namespace SharpCompress.Compressors.Deflate
s1 %= BASE;
s2 %= BASE;
}
return (s2 << 16) | s1;
return (uint)((s2 << 16) | s1);
}
}
}

View File

@@ -18,7 +18,7 @@ namespace SharpCompress.Compressors.Xz
int i = 0;
while ((LastByte & 0x80) != 0)
{
if (++i >= MaxBytes)
if (i >= MaxBytes)
throw new InvalidDataException();
LastByte = reader.ReadByte();
if (LastByte == 0)

View File

@@ -50,11 +50,11 @@ namespace SharpCompress.Compressors.Xz
private void SkipPadding()
{
int bytes = (int)(BaseStream.Position % 4);
if (bytes > 0)
int padding = (int)(_bytesRead % 4);
if (padding > 0)
{
byte[] paddingBytes = new byte[4 - bytes];
BaseStream.Read(paddingBytes, 0, paddingBytes.Length);
byte[] paddingBytes = new byte[padding];
BaseStream.Read(paddingBytes, 0, padding);
if (paddingBytes.Any(b => b != 0))
throw new InvalidDataException("Padding bytes were non-null");
}

View File

@@ -55,10 +55,10 @@ namespace SharpCompress.Compressors.Xz
private void SkipPadding()
{
int bytes = (int)(_reader.BaseStream.Position - StreamStartPosition) % 4;
if (bytes > 0)
int padding = (int)(_reader.BaseStream.Position - StreamStartPosition) % 4;
if (padding > 0)
{
byte[] paddingBytes = _reader.ReadBytes(4 - bytes);
byte[] paddingBytes = _reader.ReadBytes(padding);
if (paddingBytes.Any(b => b != 0))
throw new InvalidDataException("Padding bytes were non-null");
}

View File

@@ -93,10 +93,6 @@ namespace SharpCompress.Readers
using (FileStream fs = File.Open(destinationFileName, fm))
{
reader.WriteEntryTo(fs);
//using (Stream s = reader.OpenEntryStream())
//{
// s.TransferTo(fs);
//}
}
reader.Entry.PreserveExtractionOptions(destinationFileName, options);
}

View File

@@ -2,9 +2,9 @@
<PropertyGroup>
<AssemblyTitle>SharpCompress - Pure C# Decompression/Compression</AssemblyTitle>
<NeutralLanguage>en-US</NeutralLanguage>
<VersionPrefix>0.18.2</VersionPrefix>
<AssemblyVersion>0.18.2.0</AssemblyVersion>
<FileVersion>0.18.2.0</FileVersion>
<VersionPrefix>0.18</VersionPrefix>
<AssemblyVersion>0.18.0.0</AssemblyVersion>
<FileVersion>0.18.0.0</FileVersion>
<Authors>Adam Hathcock</Authors>
<TargetFrameworks Condition="'$(LibraryFrameworks)'==''">net45;net35;netstandard1.0;netstandard1.3</TargetFrameworks>
<TargetFrameworks Condition="'$(LibraryFrameworks)'!=''">$(LibraryFrameworks)</TargetFrameworks>

View File

@@ -17,26 +17,19 @@ namespace SharpCompress.Writers.Zip
{
public class ZipWriter : AbstractWriter
{
private readonly CompressionType compressionType;
private readonly CompressionLevel compressionLevel;
private readonly List<ZipCentralDirectoryEntry> entries = new List<ZipCentralDirectoryEntry>();
private readonly string zipComment;
private long streamPosition;
private PpmdProperties ppmdProps;
private readonly bool isZip64;
private readonly ZipWriterOptions _zipWriterOptions;
private readonly List<ZipCentralDirectoryEntry> _entries = new List<ZipCentralDirectoryEntry>();
private long _streamPosition;
private PpmdProperties _ppmdProps;
public ZipWriter(Stream destination, ZipWriterOptions zipWriterOptions)
: base(ArchiveType.Zip, zipWriterOptions)
{
zipComment = zipWriterOptions.ArchiveComment ?? string.Empty;
isZip64 = zipWriterOptions.UseZip64;
_zipWriterOptions = zipWriterOptions;
if (destination.CanSeek)
{
streamPosition = destination.Position;
_streamPosition = destination.Position;
}
compressionType = zipWriterOptions.CompressionType;
compressionLevel = zipWriterOptions.DeflateCompressionLevel;
InitalizeStream(destination);
}
@@ -44,11 +37,11 @@ namespace SharpCompress.Writers.Zip
{
get
{
if (ppmdProps == null)
if (_ppmdProps == null)
{
ppmdProps = new PpmdProperties();
_ppmdProps = new PpmdProperties();
}
return ppmdProps;
return _ppmdProps;
}
}
@@ -57,7 +50,7 @@ namespace SharpCompress.Writers.Zip
if (isDisposing)
{
ulong size = 0;
foreach (ZipCentralDirectoryEntry entry in entries)
foreach (ZipCentralDirectoryEntry entry in _entries)
{
size += entry.Write(OutputStream);
}
@@ -113,26 +106,27 @@ namespace SharpCompress.Writers.Zip
public Stream WriteToStream(string entryPath, ZipWriterEntryOptions options)
{
var compression = ToZipCompressionMethod(options.CompressionType ?? compressionType);
var compression = ToZipCompressionMethod(options.CompressionType ?? _zipWriterOptions.CompressionType);
entryPath = NormalizeFilename(entryPath);
options.ModificationDateTime = options.ModificationDateTime ?? DateTime.Now;
options.EntryComment = options.EntryComment ?? string.Empty;
var entry = new ZipCentralDirectoryEntry(compression, entryPath, (ulong)streamPosition, WriterOptions.ArchiveEncoding)
var entry = new ZipCentralDirectoryEntry(compression, entryPath, (ulong)_streamPosition, WriterOptions.ArchiveEncoding)
{
Comment = options.EntryComment,
ModificationTime = options.ModificationDateTime
};
// Use the archive default setting for zip64 and allow overrides
var useZip64 = isZip64;
var useZip64 = _zipWriterOptions.UseZip64;
if (options.EnableZip64.HasValue)
{
useZip64 = options.EnableZip64.Value;
}
var headersize = (uint)WriteHeader(entryPath, options, entry, useZip64);
streamPosition += headersize;
return new ZipWritingStream(this, OutputStream, entry, compression,
options.DeflateCompressionLevel ?? compressionLevel);
_streamPosition += headersize;
return new ZipWritingStream(this, OutputStream, entry, compression, _zipWriterOptions);
}
private string NormalizeFilename(string filename)
@@ -154,7 +148,7 @@ namespace SharpCompress.Writers.Zip
if (!OutputStream.CanSeek && useZip64)
throw new NotSupportedException("Zip64 extensions are not supported on non-seekable streams");
var explicitZipCompressionInfo = ToZipCompressionMethod(zipWriterEntryOptions.CompressionType ?? compressionType);
var explicitZipCompressionInfo = ToZipCompressionMethod(zipWriterEntryOptions.CompressionType ?? _zipWriterOptions.CompressionType);
byte[] encodedFilename = WriterOptions.ArchiveEncoding.Encode(filename);
OutputStream.Write(DataConverter.LittleEndian.GetBytes(ZipHeaderFactory.ENTRY_HEADER_BYTES), 0, 4);
@@ -179,6 +173,10 @@ namespace SharpCompress.Writers.Zip
flags |= HeaderFlags.Bit1; // eos marker
}
}
if (!string.IsNullOrEmpty(_zipWriterOptions.Password))
{
flags |= HeaderFlags.Encrypted;
}
OutputStream.Write(DataConverter.LittleEndian.GetBytes((ushort)flags), 0, 2);
OutputStream.Write(DataConverter.LittleEndian.GetBytes((ushort)explicitZipCompressionInfo), 0, 2); // zipping method
OutputStream.Write(DataConverter.LittleEndian.GetBytes(zipWriterEntryOptions.ModificationDateTime.DateTimeToDosTime()), 0, 4);
@@ -214,11 +212,11 @@ namespace SharpCompress.Writers.Zip
private void WriteEndRecord(ulong size)
{
byte[] encodedComment = WriterOptions.ArchiveEncoding.Encode(zipComment);
var zip64 = isZip64 || entries.Count > ushort.MaxValue || streamPosition >= uint.MaxValue || size >= uint.MaxValue;
byte[] encodedComment = WriterOptions.ArchiveEncoding.Encode(_zipWriterOptions.ArchiveComment ?? string.Empty);
var zip64 = _zipWriterOptions.UseZip64 || _entries.Count > ushort.MaxValue || _streamPosition >= uint.MaxValue || size >= uint.MaxValue;
var sizevalue = size >= uint.MaxValue ? uint.MaxValue : (uint)size;
var streampositionvalue = streamPosition >= uint.MaxValue ? uint.MaxValue : (uint)streamPosition;
var streampositionvalue = _streamPosition >= uint.MaxValue ? uint.MaxValue : (uint)_streamPosition;
if (zip64)
{
@@ -234,26 +232,26 @@ namespace SharpCompress.Writers.Zip
OutputStream.Write(DataConverter.LittleEndian.GetBytes((uint)0), 0, 4); // Central dir disk
// TODO: entries.Count is int, so max 2^31 files
OutputStream.Write(DataConverter.LittleEndian.GetBytes((ulong)entries.Count), 0, 8); // Entries in this disk
OutputStream.Write(DataConverter.LittleEndian.GetBytes((ulong)entries.Count), 0, 8); // Total entries
OutputStream.Write(DataConverter.LittleEndian.GetBytes((ulong)_entries.Count), 0, 8); // Entries in this disk
OutputStream.Write(DataConverter.LittleEndian.GetBytes((ulong)_entries.Count), 0, 8); // Total entries
OutputStream.Write(DataConverter.LittleEndian.GetBytes(size), 0, 8); // Central Directory size
OutputStream.Write(DataConverter.LittleEndian.GetBytes((ulong)streamPosition), 0, 8); // Disk offset
OutputStream.Write(DataConverter.LittleEndian.GetBytes((ulong)_streamPosition), 0, 8); // Disk offset
// Write zip64 end of central directory locator
OutputStream.Write(new byte[] { 80, 75, 6, 7 }, 0, 4);
OutputStream.Write(DataConverter.LittleEndian.GetBytes(0uL), 0, 4); // Entry disk
OutputStream.Write(DataConverter.LittleEndian.GetBytes((ulong)streamPosition + size), 0, 8); // Offset to the zip64 central directory
OutputStream.Write(DataConverter.LittleEndian.GetBytes((ulong)_streamPosition + size), 0, 8); // Offset to the zip64 central directory
OutputStream.Write(DataConverter.LittleEndian.GetBytes(0u), 0, 4); // Number of disks
streamPosition += recordlen + (4 + 4 + 8 + 4);
streampositionvalue = streamPosition >= uint.MaxValue ? uint.MaxValue : (uint)streampositionvalue;
_streamPosition += recordlen + (4 + 4 + 8 + 4);
streampositionvalue = _streamPosition >= uint.MaxValue ? uint.MaxValue : (uint)streampositionvalue;
}
// Write normal end of central directory record
OutputStream.Write(new byte[] { 80, 75, 5, 6, 0, 0, 0, 0 }, 0, 8);
OutputStream.Write(DataConverter.LittleEndian.GetBytes((ushort)entries.Count), 0, 2);
OutputStream.Write(DataConverter.LittleEndian.GetBytes((ushort)entries.Count), 0, 2);
OutputStream.Write(DataConverter.LittleEndian.GetBytes((ushort)_entries.Count), 0, 2);
OutputStream.Write(DataConverter.LittleEndian.GetBytes((ushort)_entries.Count), 0, 2);
OutputStream.Write(DataConverter.LittleEndian.GetBytes(sizevalue), 0, 4);
OutputStream.Write(DataConverter.LittleEndian.GetBytes((uint)streampositionvalue), 0, 4);
OutputStream.Write(DataConverter.LittleEndian.GetBytes((ushort)encodedComment.Length), 0, 2);
@@ -264,30 +262,29 @@ namespace SharpCompress.Writers.Zip
internal class ZipWritingStream : Stream
{
private readonly CRC32 crc = new CRC32();
private readonly ZipCentralDirectoryEntry entry;
private readonly Stream originalStream;
private readonly Stream writeStream;
private readonly ZipWriter writer;
private readonly ZipCompressionMethod zipCompressionMethod;
private readonly CompressionLevel compressionLevel;
private CountingWritableSubStream counting;
private ulong decompressed;
private readonly CRC32 _crc = new CRC32();
private readonly ZipCentralDirectoryEntry _entry;
private readonly Stream _originalStream;
private readonly Stream _writeStream;
private readonly ZipWriter _writer;
private readonly ZipWriterOptions _zipWriterOptions;
private readonly ZipCompressionMethod _zipCompressionMethod;
private CountingWritableSubStream _counting;
private ulong _decompressed;
// Flag to prevent throwing exceptions on Dispose
private bool limitsExceeded;
private bool isDisposed;
private bool _limitsExceeded;
internal ZipWritingStream(ZipWriter writer, Stream originalStream, ZipCentralDirectoryEntry entry,
ZipCompressionMethod zipCompressionMethod, CompressionLevel compressionLevel)
ZipCompressionMethod zipCompressionMethod,
ZipWriterOptions zipWriterOptions)
{
this.writer = writer;
this.originalStream = originalStream;
this.writer = writer;
this.entry = entry;
this.zipCompressionMethod = zipCompressionMethod;
this.compressionLevel = compressionLevel;
writeStream = GetWriteStream(originalStream);
this._writer = writer;
this._originalStream = originalStream;
this._entry = entry;
_zipWriterOptions = zipWriterOptions;
_zipCompressionMethod = zipCompressionMethod;
_writeStream = GetWriteStream(originalStream);
}
public override bool CanRead => false;
@@ -302,106 +299,109 @@ namespace SharpCompress.Writers.Zip
private Stream GetWriteStream(Stream writeStream)
{
counting = new CountingWritableSubStream(writeStream);
Stream output = counting;
switch (zipCompressionMethod)
_counting = new CountingWritableSubStream(writeStream);
Stream output = _counting;
Stream compressedStream;
switch (_zipCompressionMethod)
{
case ZipCompressionMethod.None:
{
return output;
compressedStream = output;
break;
}
case ZipCompressionMethod.Deflate:
{
return new DeflateStream(counting, CompressionMode.Compress, compressionLevel,
true);
compressedStream = new DeflateStream(_counting, CompressionMode.Compress,
_zipWriterOptions.DeflateCompressionLevel,
true);
break;
}
case ZipCompressionMethod.BZip2:
{
return new BZip2Stream(counting, CompressionMode.Compress, true);
compressedStream = new BZip2Stream(_counting, CompressionMode.Compress, true);
break;
}
case ZipCompressionMethod.LZMA:
{
counting.WriteByte(9);
counting.WriteByte(20);
counting.WriteByte(5);
counting.WriteByte(0);
_counting.WriteByte(9);
_counting.WriteByte(20);
_counting.WriteByte(5);
_counting.WriteByte(0);
LzmaStream lzmaStream = new LzmaStream(new LzmaEncoderProperties(!originalStream.CanSeek),
false, counting);
counting.Write(lzmaStream.Properties, 0, lzmaStream.Properties.Length);
return lzmaStream;
LzmaStream lzmaStream = new LzmaStream(new LzmaEncoderProperties(!_originalStream.CanSeek),
false, _counting);
_counting.Write(lzmaStream.Properties, 0, lzmaStream.Properties.Length);
compressedStream = lzmaStream;
break;
}
case ZipCompressionMethod.PPMd:
{
counting.Write(writer.PpmdProperties.Properties, 0, 2);
return new PpmdStream(writer.PpmdProperties, counting, true);
_counting.Write(_writer.PpmdProperties.Properties, 0, 2);
compressedStream = new PpmdStream(_writer.PpmdProperties, _counting, true);
break;
}
default:
{
throw new NotSupportedException("CompressionMethod: " + zipCompressionMethod);
throw new NotSupportedException("CompressionMethod: " + _zipCompressionMethod);
}
}
if (string.IsNullOrEmpty(_zipWriterOptions.Password))
{
return compressedStream;
}
var encryptionData = PkwareTraditionalEncryptionData.ForWrite(_zipWriterOptions.Password, _zipWriterOptions.ArchiveEncoding);
return new PkwareTraditionalCryptoStream(new NonDisposingStream(writeStream), encryptionData, CryptoMode.Encrypt);
}
protected override void Dispose(bool disposing)
{
if (isDisposed)
{
return;
}
isDisposed = true;
base.Dispose(disposing);
if (disposing)
{
writeStream.Dispose();
_writeStream.Dispose();
if (limitsExceeded)
if (_limitsExceeded)
{
// We have written invalid data into the archive,
// so we destroy it now, instead of allowing the user to continue
// with a defunct archive
originalStream.Dispose();
_originalStream.Dispose();
return;
}
entry.Crc = (uint)crc.Crc32Result;
entry.Compressed = counting.Count;
entry.Decompressed = decompressed;
_entry.Crc = (uint)_crc.Crc32Result;
_entry.Compressed = _counting.Count;
_entry.Decompressed = _decompressed;
var zip64 = entry.Compressed >= uint.MaxValue || entry.Decompressed >= uint.MaxValue;
var compressedvalue = zip64 ? uint.MaxValue : (uint)counting.Count;
var decompressedvalue = zip64 ? uint.MaxValue : (uint)entry.Decompressed;
var zip64 = _entry.Compressed >= uint.MaxValue || _entry.Decompressed >= uint.MaxValue;
var compressedvalue = zip64 ? uint.MaxValue : (uint)_counting.Count;
var decompressedvalue = zip64 ? uint.MaxValue : (uint)_entry.Decompressed;
if (originalStream.CanSeek)
if (_originalStream.CanSeek)
{
originalStream.Position = (long)(entry.HeaderOffset + 6);
originalStream.WriteByte(0);
originalStream.Position = (long)(entry.HeaderOffset + 14);
writer.WriteFooter(entry.Crc, compressedvalue, decompressedvalue);
_writer.WriteFooter(_entry.Crc, compressedvalue, decompressedvalue);
// Ideally, we should not throw from Dispose()
// We should not get here as the Write call checks the limits
if (zip64 && entry.Zip64HeaderOffset == 0)
if (zip64 && _entry.Zip64HeaderOffset == 0)
throw new NotSupportedException("Attempted to write a stream that is larger than 4GiB without setting the zip64 option");
// If we have pre-allocated space for zip64 data,
// fill it out, even if it is not required
if (entry.Zip64HeaderOffset != 0)
if (_entry.Zip64HeaderOffset != 0)
{
originalStream.Position = (long)(entry.HeaderOffset + entry.Zip64HeaderOffset);
originalStream.Write(DataConverter.LittleEndian.GetBytes((ushort)0x0001), 0, 2);
originalStream.Write(DataConverter.LittleEndian.GetBytes((ushort)(8 + 8)), 0, 2);
_originalStream.Position = (long)(_entry.HeaderOffset + _entry.Zip64HeaderOffset);
_originalStream.Write(DataConverter.LittleEndian.GetBytes((ushort)0x0001), 0, 2);
_originalStream.Write(DataConverter.LittleEndian.GetBytes((ushort)(8 + 8)), 0, 2);
originalStream.Write(DataConverter.LittleEndian.GetBytes(entry.Decompressed), 0, 8);
originalStream.Write(DataConverter.LittleEndian.GetBytes(entry.Compressed), 0, 8);
_originalStream.Write(DataConverter.LittleEndian.GetBytes(_entry.Decompressed), 0, 8);
_originalStream.Write(DataConverter.LittleEndian.GetBytes(_entry.Compressed), 0, 8);
}
originalStream.Position = writer.streamPosition + (long)entry.Compressed;
writer.streamPosition += (long)entry.Compressed;
_originalStream.Position = _writer._streamPosition + (long)_entry.Compressed;
_writer._streamPosition += (long)_entry.Compressed;
}
else
{
@@ -414,19 +414,19 @@ namespace SharpCompress.Writers.Zip
if (zip64)
throw new NotSupportedException("Streams larger than 4GiB are not supported for non-seekable streams");
originalStream.Write(DataConverter.LittleEndian.GetBytes(ZipHeaderFactory.POST_DATA_DESCRIPTOR), 0, 4);
writer.WriteFooter(entry.Crc,
_originalStream.Write(DataConverter.LittleEndian.GetBytes(ZipHeaderFactory.POST_DATA_DESCRIPTOR), 0, 4);
_writer.WriteFooter(_entry.Crc,
(uint)compressedvalue,
(uint)decompressedvalue);
writer.streamPosition += (long)entry.Compressed + 16;
_writer._streamPosition += (long)_entry.Compressed + 16;
}
writer.entries.Add(entry);
_writer._entries.Add(_entry);
}
}
public override void Flush()
{
writeStream.Flush();
_writeStream.Flush();
}
public override int Read(byte[] buffer, int offset, int count)
@@ -448,27 +448,27 @@ namespace SharpCompress.Writers.Zip
{
// We check the limits first, because we can keep the archive consistent
// if we can prevent the writes from happening
if (entry.Zip64HeaderOffset == 0)
if (_entry.Zip64HeaderOffset == 0)
{
// Pre-check, the counting.Count is not exact, as we do not know the size before having actually compressed it
if (limitsExceeded || ((decompressed + (uint)count) > uint.MaxValue) || (counting.Count + (uint)count) > uint.MaxValue)
if (_limitsExceeded || ((_decompressed + (uint)count) > uint.MaxValue) || (_counting.Count + (uint)count) > uint.MaxValue)
throw new NotSupportedException("Attempted to write a stream that is larger than 4GiB without setting the zip64 option");
}
decompressed += (uint)count;
crc.SlurpBlock(buffer, offset, count);
writeStream.Write(buffer, offset, count);
_decompressed += (uint)count;
_crc.SlurpBlock(buffer, offset, count);
_writeStream.Write(buffer, offset, count);
if (entry.Zip64HeaderOffset == 0)
if (_entry.Zip64HeaderOffset == 0)
{
// Post-check, this is accurate
if ((decompressed > uint.MaxValue) || counting.Count > uint.MaxValue)
if ((_decompressed > uint.MaxValue) || _counting.Count > uint.MaxValue)
{
// We have written the data, so the archive is now broken
// Throwing the exception here, allows us to avoid
// throwing an exception in Dispose() which is discouraged
// as it can mask other errors
limitsExceeded = true;
_limitsExceeded = true;
throw new NotSupportedException("Attempted to write a stream that is larger than 4GiB without setting the zip64 option");
}
}

View File

@@ -40,5 +40,10 @@ namespace SharpCompress.Writers.Zip
/// are less than 4GiB in length.
/// </summary>
public bool UseZip64 { get; set; }
/// <summary>
/// Setting a password will encrypt the zip archive with the Pkware style.
/// </summary>
public string Password { get; set; }
}
}

View File

@@ -12,11 +12,6 @@ namespace SharpCompress.Test.Xz
stream.Position = 12;
}
protected override void RewindIndexed(Stream stream)
{
stream.Position = 12;
}
private byte[] ReadBytes(XZBlock block, int bytesToRead)
{
byte[] buffer = new byte[bytesToRead];
@@ -74,25 +69,5 @@ namespace SharpCompress.Test.Xz
var sr = new StreamReader(XZBlock);
Assert.Equal(sr.ReadToEnd(), Original);
}
[Fact]
public void NoopWhenNoPadding()
{
// CompressedStream's only block has no padding.
var XZBlock = new XZBlock(CompressedStream, CheckType.CRC64, 8);
var sr = new StreamReader(XZBlock);
sr.ReadToEnd();
Assert.Equal(0L, CompressedStream.Position % 4L);
}
[Fact]
public void SkipsPaddingWhenPresent()
{
// CompressedIndexedStream's first block has 1-byte padding.
var XZBlock = new XZBlock(CompressedIndexedStream, CheckType.CRC64, 8);
var sr = new StreamReader(XZBlock);
sr.ReadToEnd();
Assert.Equal(0L, CompressedIndexedStream.Position % 4L);
}
}
}

View File

@@ -6,21 +6,11 @@ namespace SharpCompress.Test.Xz
{
public class XZIndexTests : XZTestsBase
{
protected override void RewindEmpty(Stream stream)
{
stream.Position = 12;
}
protected override void Rewind(Stream stream)
{
stream.Position = 356;
}
protected override void RewindIndexed(Stream stream)
{
stream.Position = 612;
}
[Fact]
public void RecordsStreamStartOnInit()
{
@@ -44,52 +34,12 @@ namespace SharpCompress.Test.Xz
}
[Fact]
public void ReadsNoRecord()
{
BinaryReader br = new BinaryReader(CompressedEmptyStream);
var index = new XZIndex(br, false);
index.Process();
Assert.Equal((ulong)0, index.NumberOfRecords);
}
[Fact]
public void ReadsOneRecord()
public void ReadsNumberOfRecords()
{
BinaryReader br = new BinaryReader(CompressedStream);
var index = new XZIndex(br, false);
index.Process();
Assert.Equal((ulong)1, index.NumberOfRecords);
}
[Fact]
public void ReadsMultipleRecords()
{
BinaryReader br = new BinaryReader(CompressedIndexedStream);
var index = new XZIndex(br, false);
index.Process();
Assert.Equal((ulong)2, index.NumberOfRecords);
}
[Fact]
public void ReadsFirstRecord()
{
BinaryReader br = new BinaryReader(CompressedStream);
var index = new XZIndex(br, false);
index.Process();
Assert.Equal((ulong)OriginalBytes.Length, index.Records[0].UncompressedSize);
}
[Fact]
public void SkipsPadding()
{
// Index with 3-byte padding.
using (Stream badStream = new MemoryStream(new byte[] { 0x00, 0x01, 0x10, 0x80, 0x01, 0x00, 0x00, 0x00, 0xB1, 0x01, 0xD9, 0xC9, 0xFF }))
{
BinaryReader br = new BinaryReader(badStream);
var index = new XZIndex(br, false);
index.Process();
Assert.Equal(0L, badStream.Position % 4L);
}
Assert.Equal(index.NumberOfRecords, (ulong)1);
}
}
}

View File

@@ -4,19 +4,8 @@ using Xunit;
namespace SharpCompress.Test.Xz
{
public class XZStreamTests : XZTestsBase
public class XZStreamReaderTests : XZTestsBase
{
[Fact]
public void CanReadEmptyStream()
{
XZStream xz = new XZStream(CompressedEmptyStream);
using (var sr = new StreamReader(xz))
{
string uncompressed = sr.ReadToEnd();
Assert.Equal(OriginalEmpty, uncompressed);
}
}
[Fact]
public void CanReadStream()
{
@@ -24,18 +13,7 @@ namespace SharpCompress.Test.Xz
using (var sr = new StreamReader(xz))
{
string uncompressed = sr.ReadToEnd();
Assert.Equal(Original, uncompressed);
}
}
[Fact]
public void CanReadIndexedStream()
{
XZStream xz = new XZStream(CompressedIndexedStream);
using (var sr = new StreamReader(xz))
{
string uncompressed = sr.ReadToEnd();
Assert.Equal(OriginalIndexed, uncompressed);
Assert.Equal(uncompressed, Original);
}
}
}

View File

@@ -7,14 +7,7 @@ namespace SharpCompress.Test.Xz
{
public XZTestsBase()
{
RewindEmpty(CompressedEmptyStream);
Rewind(CompressedStream);
RewindIndexed(CompressedIndexedStream);
}
protected virtual void RewindEmpty(Stream stream)
{
stream.Position = 0;
}
protected virtual void Rewind(Stream stream)
@@ -22,22 +15,6 @@ namespace SharpCompress.Test.Xz
stream.Position = 0;
}
protected virtual void RewindIndexed(Stream stream)
{
stream.Position = 0;
}
protected Stream CompressedEmptyStream { get; } = new MemoryStream(CompressedEmpty);
protected static byte[] CompressedEmpty { get; } = new byte[] {
0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00, 0x01, 0x69, 0x22, 0xde, 0x36, 0x00, 0x00, 0x00, 0x00,
0x1c, 0xdf, 0x44, 0x21, 0x90, 0x42, 0x99, 0x0d, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x59, 0x5a
};
protected static byte[] OriginalEmptyBytes => Encoding.ASCII.GetBytes(OriginalEmpty);
protected static string OriginalEmpty { get; } = string.Empty;
protected Stream CompressedStream { get; } = new MemoryStream(Compressed);
protected static byte[] Compressed { get; } = new byte[] {
@@ -88,54 +65,5 @@ namespace SharpCompress.Test.Xz
"The eager children cry.\r\n" +
"\"Why, Mary loves the lamb, you know.\"\r\n" +
"The teacher did reply.";
protected Stream CompressedIndexedStream { get; } = new MemoryStream(CompressedIndexed);
protected static byte[] CompressedIndexed { get; } = new byte[] {
0xfd, 0x37, 0x7a, 0x58, 0x5a, 0x00, 0x00, 0x01, 0x69, 0x22, 0xde, 0x36, 0x03, 0xc0, 0xe3, 0x02,
0x80, 0x20, 0x21, 0x01, 0x00, 0x00, 0x00, 0x00, 0x7e, 0xe5, 0xd7, 0x32, 0xe0, 0x0f, 0xff, 0x01,
0x5b, 0x5d, 0x00, 0x26, 0x98, 0x4a, 0x47, 0xc6, 0x6a, 0x27, 0xd7, 0x36, 0x7a, 0x05, 0xb9, 0x4f,
0xd7, 0xde, 0x3a, 0x0e, 0xee, 0x1b, 0xd7, 0x81, 0xe9, 0xf5, 0x90, 0x1e, 0xd5, 0x9e, 0x88, 0x32,
0x1c, 0x7b, 0x43, 0x84, 0x02, 0x58, 0x92, 0xcf, 0x97, 0xfc, 0xae, 0x01, 0x83, 0x23, 0x48, 0x93,
0xc6, 0x56, 0xcc, 0x6d, 0xb1, 0x23, 0x10, 0x24, 0x3b, 0x9e, 0x06, 0xaa, 0xc0, 0xce, 0x86, 0x0a,
0xb7, 0x9f, 0x99, 0x61, 0xbe, 0x3b, 0x6d, 0xfe, 0x60, 0xef, 0x14, 0x35, 0x7f, 0x21, 0xe8, 0x96,
0x0e, 0xbd, 0x41, 0x7c, 0x65, 0x89, 0x96, 0x28, 0x5e, 0x85, 0xa6, 0x4b, 0xf3, 0xf9, 0xf8, 0x25,
0x31, 0x4a, 0xbb, 0x72, 0xce, 0xcf, 0x53, 0xdf, 0x13, 0x42, 0x2d, 0xbc, 0x95, 0xa5, 0x6d, 0xc4,
0x8c, 0x72, 0x99, 0xe8, 0x9a, 0xcf, 0x80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 0x69, 0x36, 0xde, 0xea, 0x53, 0x8c,
0x89, 0xd7, 0x22, 0xa6, 0xf7, 0xa8, 0x4c, 0x72, 0x6c, 0x80, 0x69, 0x01, 0xb2, 0xa7, 0xe8, 0x8b,
0x94, 0xaf, 0x0e, 0x47, 0x58, 0x1d, 0x0e, 0x5c, 0x7c, 0x33, 0x9f, 0x21, 0x17, 0x2c, 0x4f, 0x3d,
0x72, 0xff, 0xcf, 0x7a, 0x4f, 0x82, 0x5b, 0x85, 0x28, 0x70, 0xf4, 0x8c, 0x81, 0x41, 0xb8, 0x20,
0x5c, 0x3e, 0x02, 0x5e, 0x5a, 0x61, 0xbb, 0x2f, 0x64, 0xc5, 0x4e, 0x53, 0xe4, 0xca, 0xe4, 0xd9,
0x75, 0xaf, 0x15, 0x4d, 0xff, 0x01, 0xec, 0x13, 0x4a, 0x70, 0x00, 0x04, 0xf9, 0xfa, 0x00, 0x00,
0x99, 0x57, 0xc4, 0x96, 0x00, 0x02, 0xf7, 0x02, 0x80, 0x20, 0xdf, 0x01, 0x8d, 0x02, 0x00, 0x00,
0x4c, 0x41, 0xe6, 0xa1, 0x9b, 0xe3, 0x51, 0x40, 0x03, 0x00, 0x00, 0x00, 0x00, 0x01, 0x59, 0x5a
};
protected static byte[] OriginalIndexedBytes => Encoding.ASCII.GetBytes(OriginalIndexed);
protected static string OriginalIndexed { get; } = Original + Original + Original + Original + Original + Original + Original + Original + Original;
}
}

View File

@@ -1,4 +1,9 @@
using SharpCompress.Common;
using System.IO;
using SharpCompress.Common;
using SharpCompress.Readers;
using SharpCompress.Readers.Zip;
using SharpCompress.Writers;
using SharpCompress.Writers.Zip;
using Xunit;
namespace SharpCompress.Test.Zip
@@ -48,5 +53,41 @@ namespace SharpCompress.Test.Zip
{
Assert.Throws<InvalidFormatException>(() => Write(CompressionType.Rar, "Zip.ppmd.noEmptyDirs.zip", "Zip.ppmd.noEmptyDirs.zip"));
}
[Fact]
public void Zip_BZip2_PkwareEncryption_Write()
{
ResetScratch();
using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH_FILES_PATH, "Zip.pkware.zip")))
{
using (var writer = new ZipWriter(stream, new ZipWriterOptions(CompressionType.BZip2)
{
Password = "test"
}))
{
writer.WriteAll(ORIGINAL_FILES_PATH, "*", SearchOption.AllDirectories);
}
}
using (Stream stream = File.OpenRead(Path.Combine(SCRATCH_FILES_PATH, "Zip.pkware.zip")))
using (var reader = ZipReader.Open(stream, new ReaderOptions()
{
Password = "test"
}))
{
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
Assert.Equal(CompressionType.BZip2, reader.Entry.CompressionType);
reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions()
{
ExtractFullPath = true,
Overwrite = true
});
}
}
}
VerifyFiles();
}
}
}

Binary file not shown.