public async Task <ActionResult> Decompress([FromForm] IFormFile file) { string path = _env.ContentRootPath; string OriginalName = file.FileName; OriginalName = OriginalName.Substring(0, OriginalName.Length - 4); string downloadPath = path + @"\Compressions\" + OriginalName; byte[] FileBytes; try { if (file != null) { using (FileStream fs = System.IO.File.Create(downloadPath)) { await file.CopyToAsync(fs); } LZW Compressor = new LZW(downloadPath); FileBytes = Compressor.Decompress(downloadPath, 100); return(File(FileBytes, "text/plain", Compressor.Name));; } else { return(StatusCode(500)); } } catch (Exception) { return(StatusCode(500)); } }
public void Post([FromForm(Name = "file")] IFormFile file, string name, string method) { string folder = @"C:\Compressions\"; string fullPath = folder + file.FileName; byte[] txt = new byte[file.Length]; using (FileStream fs = new FileStream(fullPath, FileMode.Open)) { int count; // actual number of bytes read int sum = 0; // total number of bytes read // read until Read method returns 0 (end of the stream has been reached) while ((count = fs.Read(txt, sum, txt.Length - sum)) > 0) { sum += count; // sum is a buffer offset for next reading } } if (method.ToLower().Equals("huffman")) { Huffman huffmanMethods = new Huffman(); huffmanMethods.DecodeFile(txt, name, file.FileName); } else if (method.ToLower().Equals("lzw")) { LZW lzwMethods = new LZW(); lzwMethods.Decompress(txt, name); } }
private static void RunLZW(string text) { List <int> compressed = LZW.Compress(text); Console.WriteLine(string.Join(", ", compressed)); string decompressed = LZW.Decompress(compressed); Console.WriteLine(decompressed); Console.WriteLine($"text.Length: {text.Length}, compressed.Length: {compressed.Count}"); }
public void Decompress1_Decompresses_Decompressed() { var lzw = new LZW(); var input = new MemoryStream(new byte[] { 1, 0, 224, 48, 48, 32 }); var output = new MemoryStream(); byte[] expected = { 1, 2, 1, 2, 1, 2, 1, 2 }; lzw.Decompress(input, output); byte[] result = output.ToArray(); CollectionAssert.AreEqual(expected, result); }
public void Decompress_Decompresses_Decompressed() { var lzw = new LZW(); var input = new MemoryStream(new byte[] { 1, 0, 192, 192, 32, 64, 12, 15, 1, 3, 0, 193, 16, 16, 16 }); var output = new MemoryStream(); byte[] expected = { 1, 2, 5, 1, 7, 2, 6, 1, 2, 5, 2, 7, 1, 2, 1 }; lzw.Decompress(input, output); byte[] result = output.ToArray(); CollectionAssert.AreEqual(expected, result); }
public void Decompress2_Decompresses_Decompressed() { var lzw = new LZW(); var input = new MemoryStream(new byte[] { 58, 26, 142, 98, 24, 9, 148, 66, 102, 49, 29, 14, 166, 227, 17, 190, 15, 9, 53, 27, 79, 66, 19, 172, 32, 232, 102, 58, 65, 33, 16, 67, 81, 144, 217, 26, 49, 27, 92, 192 }); var output = new MemoryStream(); byte[] expected = { 115, 105, 114, 32, 115, 105, 100, 32, 101, 97, 115, 116, 109, 97, 110, 32, 101, 97, 115, 105, 108, 121, 32, 116, 101, 97, 115, 101, 115, 32, 115, 101, 97, 32, 115, 105, 99, 107, 32, 115, 101, 97, 108, 115 }; lzw.Decompress(input, output); byte[] result = output.ToArray(); CollectionAssert.AreEqual(expected, result); }
public void TestLZW() { Random r = new Random(); StringBuilder testStr = new StringBuilder(); for (int i = 0; i < 1000000; ++i) { testStr.Append(((char)r.Next(32, 126))); } byte[] inputBytes = Encoding.ASCII.GetBytes(testStr.ToString()); byte[] compressed = LZW.Compress(testStr.ToString()); string decompressedStr = LZW.Decompress(compressed); //string decompressedStr = Encoding.ASCII.GetString(decompressedBytes); //CollectionAssert.AreEqual(inputBytes, decompressedBytes); Assert.AreEqual(testStr.ToString(), decompressedStr); }
public static MemoryStream ReadEntry( Stream input, ArchiveFile.Entry entry, ArchiveCompressionMode mode, Endian endian) { switch (mode) { case ArchiveCompressionMode.Bogocrypt1: { var output = new MemoryStream(); var key = entry.BogocryptKey; long remaining = entry.Length; var block = new byte[1024]; while (remaining > 0) { var blockLength = (int)Math.Min(block.Length, remaining + 3 & ~3); var actualBlockLength = (int)Math.Min(block.Length, remaining); if (blockLength == 0) { throw new InvalidOperationException(); } if (input.Read(block, 0, blockLength) < actualBlockLength) { throw new EndOfStreamException(); } key = ArchiveFile.Bogocrypt1(block, 0, blockLength, key); output.Write(block, 0, actualBlockLength); remaining -= blockLength; } output.Position = 0; return(output); } case ArchiveCompressionMode.LZW: { var output = new MemoryStream(); LZW.Decompress(input, entry.Length, output, endian); output.Position = 0; return(output); } case ArchiveCompressionMode.MiniZ: { ISAAC isaac = null; var outputBytes = new byte[entry.Length]; var outputOffset = 0; var blockBytes = new byte[0x800]; long remaining = entry.Length; bool isCompressed = true; bool isLastBlock; do { var blockFlags = input.ReadValueU32(endian); var blockLength = (int)(blockFlags & ~0x80000000u); isLastBlock = (blockFlags & 0x80000000u) != 0; if (blockLength > blockBytes.Length) { throw new InvalidOperationException(); } if (input.Read(blockBytes, 0, blockLength) != blockLength) { throw new EndOfStreamException(); } if (isCompressed == false || (isLastBlock == false && blockLength == 1024)) { isCompressed = false; if (isaac == null) { isaac = entry.GetISAAC(); } int seed = 0; for (int o = 0; o < blockLength; o++) { if ((o & 3) != 0) { seed >>= 8; } else { seed = isaac.Value(); } blockBytes[o] ^= (byte)seed; } Array.Copy(blockBytes, 0, outputBytes, outputOffset, blockLength); outputOffset += blockLength; remaining -= blockLength; } else { using (var temp = new MemoryStream(blockBytes, false)) { var zlib = new InflaterInputStream(temp, new Inflater(true)); var read = zlib.Read(outputBytes, outputOffset, (int)Math.Min(remaining, 1024)); outputOffset += read; remaining -= read; } } }while (isLastBlock == false); return(new MemoryStream(outputBytes)); } case ArchiveCompressionMode.Bogocrypt2: { var blockBytes = new byte[1024]; var output = new MemoryStream(); long remaining = entry.Length; while (remaining >= 4) { var blockLength = (int)Math.Min(blockBytes.Length, remaining); if (input.Read(blockBytes, 0, blockLength) != blockLength) { throw new EndOfStreamException(); } ArchiveFile.Bogocrypt2(blockBytes, 0, blockLength); output.Write(blockBytes, 0, blockLength); remaining -= blockLength; } if (remaining > 0) { output.WriteFromStream(input, remaining); } output.Position = 0; return(output); } default: { throw new NotSupportedException(); } } }