public void writeLZ4NHStream(RootEntry root, Stream s) { using (var xFs = new Lz4CompressionStream(s, 1 << 18, Lz4Mode.HighCompression)) { root.Write(xFs); } }
public static CompressResult Compress([NotNull] Stream inputFile, [NotNull] Stream compressFile = null, int blockSize = 0x100000, Lz4Mode mode = 0, Action <CompressResult> blockCallback = null) { long position; if (inputFile == null) { throw new ArgumentNullException(nameof(inputFile)); } if (compressFile == null) { throw new ArgumentNullException(nameof(compressFile)); } CompressResult result = new CompressResult(); byte[] buffer = new byte[blockSize]; try { position = compressFile.Position; } catch (Exception) { position = -1L; result.CompressedBytes = -1L; } using (Lz4CompressionStream stream = new Lz4CompressionStream(compressFile, 0x100000, mode)) { goto Label_00E9; Label_006D: int num2 = inputFile.Read(buffer, 0, buffer.Length); if (num2 == 0) { return(result); } result.Bytes += num2; stream.Write(buffer, 0, num2); if (position != -1L) { long num3 = compressFile.Position; result.CompressedBytes += num3 - position; position = num3; } if (blockCallback != null) { blockCallback(result); } Label_00E9: goto Label_006D; } }
public static CompressResult Compress([NotNull] Stream inputFile, [NotNull] Stream compressFile = null, int blockSize = 0x100000, Lz4Mode mode = 0, Action<CompressResult> blockCallback = null) { long position; if (inputFile == null) { throw new ArgumentNullException("inputFile"); } if (compressFile == null) { throw new ArgumentNullException("compressFile"); } CompressResult result = new CompressResult(); byte[] buffer = new byte[blockSize]; try { position = compressFile.Position; } catch (Exception) { position = -1L; result.CompressedBytes = -1L; } using (Lz4CompressionStream stream = new Lz4CompressionStream(compressFile, 0x100000, mode)) { goto Label_00E9; Label_006D: int num2 = inputFile.Read(buffer, 0, buffer.Length); if (num2 == 0) { return result; } result.Bytes += num2; stream.Write(buffer, 0, num2); if (position != -1L) { long num3 = compressFile.Position; result.CompressedBytes += num3 - position; position = num3; } if (blockCallback != null) { blockCallback(result); } Label_00E9: goto Label_006D; } }
private void AddEntry(Stream stream, Lz4Mode mode, int blockSize, Lz4PackageEntry entry) { using (FileStream stream2 = OpenWrite(blockSize)) { entry.Entry = stream2.Position; using (Lz4CompressionStream stream3 = new Lz4CompressionStream(stream2, blockSize, mode)) { int num; byte[] buffer = new byte[blockSize]; while ((num = stream.Read(buffer, 0, buffer.Length)) > 0) { entry.OriginSize += num; stream3.Write(buffer, 0, num); } } entry.CompressedSize = stream2.Position - entry.Entry; } }
public void StreamCompression_FileStream_1Mb() { var filename = System.IO.Path.GetRandomFileName(); int currentSz = 0; int sz = 0; int maxSz = 1024 * 1024 * 10; List <uint> hashes = new List <uint> (1024); byte[] buffer; try { // create file using (var stream = new Lz4CompressionStream(new System.IO.FileStream(filename, System.IO.FileMode.Create), 1 << 18, Lz4Mode.HighCompression, true)) { while (currentSz < maxSz) { buffer = GetStringsAsByte(1024); stream.Write(buffer, 0, buffer.Length); currentSz += buffer.Length; // save hash hashes.Add(SimpleHash(buffer)); } } // read buffer = new byte[1024]; int ix = 0; using (var stream = new Lz4DecompressionStream(new System.IO.FileStream(filename, System.IO.FileMode.Open), true)) { while ((sz = stream.Read(buffer, 0, buffer.Length)) > 0) { // check hash Assert.IsTrue(SimpleHash(buffer) == hashes[ix++], "Hash mismatch"); } } } finally { System.IO.File.Delete(filename); } }
public void StreamCompression_SimpleTest() { var txt = sampleTxt + sampleTxt + sampleTxt + sampleTxt + sampleTxt + sampleTxt; var mem = new System.IO.MemoryStream(); using (var stream = new Lz4CompressionStream(mem, 1 << 18, Lz4Mode.HighCompression)) { var b = System.Text.Encoding.UTF8.GetBytes(txt); stream.Write(b, 0, b.Length); } // reset stream position mem.Position = 0; using (var stream = new Lz4DecompressionStream(mem)) { var b = new byte[1024]; int sz = stream.Read(b, 0, b.Length); // validate Assert.IsTrue(txt == System.Text.Encoding.UTF8.GetString(b, 0, sz)); } }