public void ReadAndWriteZip64NonSeekable() { MemoryStream msw = new MemoryStreamWithoutSeek(); using (ZipOutputStream outStream = new ZipOutputStream(msw)) { outStream.UseZip64 = UseZip64.On; outStream.IsStreamOwner = false; outStream.PutNextEntry(new ZipEntry("StripedMarlin")); outStream.WriteByte(89); outStream.PutNextEntry(new ZipEntry("StripedMarlin2")); outStream.WriteByte(89); outStream.Close(); } Assert.IsTrue(ZipTesting.TestArchive(msw.ToArray())); msw.Position = 0; using (ZipInputStream zis = new ZipInputStream(msw)) { while (zis.GetNextEntry() != null) { int len = 0; int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; while ((len = zis.Read(buffer, 0, bufferSize)) > 0) { // Reading the data is enough } } } }
public void Zip64Descriptor() { MemoryStream msw = new MemoryStreamWithoutSeek(); var outStream = new ZipOutputStream(msw); outStream.UseZip64 = UseZip64.Off; outStream.IsStreamOwner = false; outStream.PutNextEntry(new ZipEntry("StripedMarlin")); outStream.WriteByte(89); outStream.Close(); Assert.IsTrue(ZipTesting.TestArchive(msw.ToArray())); msw = new MemoryStreamWithoutSeek(); outStream = new ZipOutputStream(msw); outStream.UseZip64 = UseZip64.On; outStream.IsStreamOwner = false; outStream.PutNextEntry(new ZipEntry("StripedMarlin")); outStream.WriteByte(89); outStream.Close(); Assert.IsTrue(ZipTesting.TestArchive(msw.ToArray())); }
public void ShouldThrowDescriptiveExceptionOnUncompressedDescriptorEntry() { using var ms = new MemoryStreamWithoutSeek(); using (var zos = new ZipOutputStream(ms)) { zos.IsStreamOwner = false; var entry = new ZipEntry("testentry"); entry.CompressionMethod = CompressionMethod.Stored; entry.Flags |= (int)GeneralBitFlags.Descriptor; zos.PutNextEntry(entry); zos.Write(new byte[1], 0, 1); zos.CloseEntry(); } // Patch the Compression Method, since ZipOutputStream automatically changes it to Deflate when descriptors are used ms.Seek(8, SeekOrigin.Begin); ms.WriteByte((byte)CompressionMethod.Stored); ms.Seek(0, SeekOrigin.Begin); using (var zis = new ZipInputStream(ms)) { zis.IsStreamOwner = false; var buf = new byte[32]; zis.GetNextEntry(); Assert.Throws(typeof(StreamUnsupportedException), () => { zis.Read(buf, 0, buf.Length); }); } }
protected byte[] MakeInMemoryZip(bool withSeek, params object[] createSpecs) { MemoryStream ms; if (withSeek) { ms = new MemoryStream(); } else { ms = new MemoryStreamWithoutSeek(); } using (ZipOutputStream outStream = new ZipOutputStream(ms)) { for (int counter = 0; counter < createSpecs.Length; ++counter) { var info = createSpecs[counter] as RuntimeInfo; outStream.Password = info.Password; if (info.Method != CompressionMethod.Stored) { outStream.SetLevel(info.CompressionLevel); // 0 - store only to 9 - means best compression } string entryName; if (info.IsDirectory) { entryName = "dir" + counter + "/"; } else { entryName = "entry" + counter + ".tst"; } var entry = new ZipEntry(entryName); entry.CompressionMethod = info.Method; if (info.Crc >= 0) { entry.Crc = info.Crc; } outStream.PutNextEntry(entry); if (info.Size > 0) { outStream.Write(info.Original, 0, info.Original.Length); } } } return(ms.ToArray()); }
protected byte[] MakeInMemoryZip(ref byte[] original, CompressionMethod method, int compressionLevel, int size, string password, bool withSeek) { MemoryStream ms; if (withSeek) { ms = new MemoryStream(); } else { ms = new MemoryStreamWithoutSeek(); } using (ZipOutputStream outStream = new ZipOutputStream(ms)) { outStream.Password = password; if (method != CompressionMethod.Stored) { outStream.SetLevel(compressionLevel); // 0 - store only to 9 - means best compression } var entry = new ZipEntry("dummyfile.tst"); entry.CompressionMethod = method; outStream.PutNextEntry(entry); if (size > 0) { var rnd = new Random(); original = new byte[size]; rnd.NextBytes(original); // Although this could be written in one chunk doing it in lumps // throws up buffering problems including with encryption the original // source for this change. int index = 0; while (size > 0) { int count = (size > 0x200) ? 0x200 : size; outStream.Write(original, index, count); size -= 0x200; index += count; } } } return(ms.ToArray()); }
public void EntryWithNoDataAndZip64() { MemoryStream msw = new MemoryStreamWithoutSeek(); var outStream = new ZipOutputStream(msw); outStream.IsStreamOwner = false; var ze = new ZipEntry("Striped Marlin"); ze.ForceZip64(); ze.Size = 0; outStream.PutNextEntry(ze); outStream.CloseEntry(); outStream.Finish(); outStream.Close(); Assert.IsTrue(ZipTesting.TestArchive(msw.ToArray())); }
public void StoredNonSeekableKnownSizeNoCrcEncrypted() { // This cant be stored directly as the crc is not known const int TargetSize = 24692; const string Password = "******"; MemoryStream ms = new MemoryStreamWithoutSeek(); using (ZipOutputStream outStream = new ZipOutputStream(ms)) { outStream.Password = Password; outStream.IsStreamOwner = false; var entry = new ZipEntry("dummyfile.tst"); entry.CompressionMethod = CompressionMethod.Stored; // The bit thats in question is setting the size before its added to the archive. entry.Size = TargetSize; outStream.PutNextEntry(entry); Assert.AreEqual(CompressionMethod.Deflated, entry.CompressionMethod, "Entry should be stored"); Assert.AreEqual(-1, entry.CompressedSize, "Compressed size should be known"); var rnd = new Random(); int size = TargetSize; byte[] original = new byte[size]; rnd.NextBytes(original); // Although this could be written in one chunk doing it in lumps // throws up buffering problems including with encryption the original // source for this change. int index = 0; while (size > 0) { int count = (size > 0x200) ? 0x200 : size; outStream.Write(original, index, count); size -= 0x200; index += count; } } Assert.IsTrue(ZipTesting.TestArchive(ms.ToArray(), Password)); }
public void TestZeroByteWrite() { using (var ms = new MemoryStreamWithoutSeek()) { using (var outStream = new ZipOutputStream(ms) { IsStreamOwner = false }) { var ze = new ZipEntry("Striped Marlin"); outStream.PutNextEntry(ze); var buffer = Array.Empty <byte>(); outStream.Write(buffer, 0, 0); } ms.Seek(0, SeekOrigin.Begin); using (var inStream = new ZipInputStream(ms) { IsStreamOwner = false }) { int extractCount = 0; byte[] decompressedData = new byte[100]; while (inStream.GetNextEntry() != null) { while (true) { int numRead = inStream.Read(decompressedData, extractCount, decompressedData.Length); if (numRead <= 0) { break; } extractCount += numRead; } } Assert.Zero(extractCount, "No data should be read from empty entries"); } } }
public void StoredNonSeekableConvertToDeflate() { var ms = new MemoryStreamWithoutSeek(); var outStream = new ZipOutputStream(ms); outStream.SetLevel(8); Assert.AreEqual(8, outStream.GetLevel(), "Compression level invalid"); var entry = new ZipEntry("1.tst"); entry.CompressionMethod = CompressionMethod.Stored; outStream.PutNextEntry(entry); Assert.AreEqual(0, outStream.GetLevel(), "Compression level invalid"); AddRandomDataToEntry(outStream, 100); entry = new ZipEntry("2.tst"); entry.CompressionMethod = CompressionMethod.Deflated; outStream.PutNextEntry(entry); Assert.AreEqual(8, outStream.GetLevel(), "Compression level invalid"); AddRandomDataToEntry(outStream, 100); outStream.Close(); }