public static byte[] ToCompressedBytes <T>(T packet) where T : IPacket { var packetBytes = ToBytes(packet); var compressedPacketBytes = new byte[packetBytes.Length]; HuffmanCompressor.Compress(packetBytes, out compressedPacketBytes); return(compressedPacketBytes); }
static void Main(string[] args) { HuffmanCompressor oHuffmanCompressor = new HuffmanCompressor(); oHuffmanCompressor.Compress("test.txt"); Console.WriteLine("compressed"); oHuffmanCompressor.Decompress("test.txt"); Console.WriteLine("decompressed"); Console.ReadLine(); }
public static void CompressingPhrase(string uncompressedText, string expectedCompressedText) { //Arrange var sorter = new BubbleSorter <HuffmanCompressor.ListNode>(); var translator = new Translator(); var huffman = new HuffmanCompressor(sorter, translator); //Act var(compressedText, decompressionKeys) = huffman.Compress(uncompressedText); var decompressedText = translator.Translate(compressedText, decompressionKeys); //Assert Assert.AreEqual(expectedCompressedText, compressedText); Assert.AreEqual(uncompressedText, decompressedText); }
public void DecompressedTextTheSameAsOriginal([Random(0, 1000, 100, Distinct = true)] int length) { //Arrange var sorter = new BubbleSorter <HuffmanCompressor.ListNode>(); var translator = new Translator(); var huffman = new HuffmanCompressor(sorter, translator); var text = Randomizer.CreateRandomizer().GetString(length); //Act var(compressedText, decompressionKeys) = huffman.Compress(text); var decompressedText = translator.Translate(compressedText, decompressionKeys); //Assert Assert.AreEqual(text, decompressedText); }
public IActionResult Compress([FromForm] IFormFile file, string name) { try { string path = env.ContentRootPath + "\\" + file.FileName; using var saver = new FileStream(path, FileMode.Create); file.CopyTo(saver); saver.Close(); using var fileWritten = new FileStream(path, FileMode.OpenOrCreate); using var reader = new BinaryReader(fileWritten); byte[] buffer = new byte[0]; while (fileWritten.Position < fileWritten.Length) { int index = buffer.Length; Array.Resize <byte>(ref buffer, index + 100000); byte[] aux = reader.ReadBytes(100000); aux.CopyTo(buffer, index); } reader.Close(); fileWritten.Close(); for (int i = 0; i < buffer.Length; i++) { if (buffer[i] == 0) { Array.Resize <byte>(ref buffer, i); break; } } if (buffer.Length > 0) { var compressor = new HuffmanCompressor(env.ContentRootPath); path = compressor.Compress(buffer, file.FileName, name); var fileStream = new FileStream(path, FileMode.OpenOrCreate); return(File(fileStream, "text/plain")); } else { return(StatusCode(500, "El archivo está vacío")); } } catch { return(StatusCode(500)); } }
public byte[] Compress(byte[] data) { byte[] patternPacked = patternPacker.Compress(data); byte[] huffmanPacked = huffmanPacker.Compress(patternPacked); return(huffmanPacked); }