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) { RuntimeInfo 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"; } ZipEntry 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(); }
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; ZipEntry 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"); System.Random 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 StoredNonSeekableConvertToDeflate() { MemoryStreamWithoutSeek ms = new MemoryStreamWithoutSeek(); ZipOutputStream outStream = new ZipOutputStream(ms); outStream.SetLevel(8); Assert.AreEqual(8, outStream.GetLevel(), "Compression level invalid"); ZipEntry 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(); }
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 } ZipEntry entry = new ZipEntry("dummyfile.tst"); entry.CompressionMethod = method; outStream.PutNextEntry(entry); if (size > 0) { System.Random 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 Zip64Descriptor() { MemoryStream msw = new MemoryStreamWithoutSeek(); ZipOutputStream 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 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 EntryWithNoDataAndZip64() { MemoryStream msw = new MemoryStreamWithoutSeek(); ZipOutputStream outStream = new ZipOutputStream(msw); outStream.IsStreamOwner = false; ZipEntry 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())); }
protected byte[] MakeInMemoryZip(ref byte[] original, CompressionMethod method, int compressionLevel, int size, string password, bool withSeek) { MemoryStream ms; if (withSeek == true) { 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 } ZipEntry entry = new ZipEntry("dummyfile.tst"); entry.CompressionMethod = method; outStream.PutNextEntry(entry); if (size > 0) { System.Random rnd = new Random(); original = new byte[size]; rnd.NextBytes(original); outStream.Write(original, 0, original.Length); } } return ms.ToArray(); }
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(); var ms = new MemoryStream(msw.ToArray()); using (var zf = new ZipFile(ms)) { Assert.IsTrue(zf.TestArchive(true)); } msw = new MemoryStreamWithoutSeek(); outStream = new ZipOutputStream(msw); outStream.UseZip64 = UseZip64.On; outStream.IsStreamOwner = false; outStream.PutNextEntry(new ZipEntry("StripedMarlin")); outStream.WriteByte(89); outStream.Close(); ms = new MemoryStream(msw.ToArray()); using (var zf = new ZipFile(ms)) { Assert.IsTrue(zf.TestArchive(true)); } }
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(); var ms = new MemoryStream(msw.ToArray()); using (var zf = new ZipFile(ms)) { Assert.IsTrue(zf.TestArchive(true)); } }