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 Compress2_Compresses_Compressed() { var lzw = new LZW(); var input = new MemoryStream(new byte[] { 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 }); var output = new MemoryStream(); byte[] expected = { 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 }; lzw.Compress(input, output); byte[] result = output.ToArray(); CollectionAssert.AreEqual(expected, result); }
public void Compress1_Compresses_Compressed() { var lzw = new LZW(); var input = new MemoryStream(new byte[] { 1, 2, 1, 2, 1, 2, 1, 2 }); var output = new MemoryStream(); byte[] expected = { 1, 0, 224, 48, 48, 32 }; lzw.Compress(input, output); byte[] result = output.ToArray(); CollectionAssert.AreEqual(expected, result); }
public void Compress_Compresses_Compressed() { var lzw = new LZW(); var input = new MemoryStream(new byte[] { 1, 2, 5, 1, 7, 2, 6, 1, 2, 5, 2, 7, 1, 2, 1 }); var output = new MemoryStream(); byte[] expected = { 1, 0, 192, 192, 32, 64, 12, 15, 1, 3, 0, 193, 16, 16, 16 }; lzw.Compress(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 async Task <ActionResult> Compress(string name, [FromForm] IFormFile file) { string path = _env.ContentRootPath; string OriginalName = file.FileName; string uploadPath = path + @"\Uploads\" + OriginalName; string downloadPath = path + @"\Compressions\" + name; byte[] FileBytes; double originalSize; try { if (file != null) { using (FileStream fs = System.IO.File.Create(uploadPath)) { await file.CopyToAsync(fs); originalSize = fs.Length; } LZW Compressor = new LZW(uploadPath); FileBytes = Compressor.Compress(uploadPath, OriginalName, 100); double compressedSize = FileBytes.Length; Compressor.UpdateCompressions(path, OriginalName, uploadPath, originalSize, compressedSize); return(File(FileBytes, "text/plain", name + ".lzw")); } else { return(StatusCode(500)); } } catch (Exception) { return(StatusCode(500)); } }