public void CompressFile(string path, string originalName) { LZW lZW = new LZW(); byte[] buffer; using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate)) { buffer = new byte[fs.Length]; using (var br = new BinaryReader(fs)) { br.Read(buffer, 0, (int)fs.Length); } } byte[] result = lzw.EncodeData(buffer); for (int i = 1; File.Exists(CompressedFilePath); i++) { var split = CompressedFileName.Split(".lzw"); if (split[0].Contains("(")) { var split2 = split[0].Split("("); CompressedFileName = split2[0] + "(" + i + ")" + ".lzw"; } else { CompressedFileName = split[0] + "(" + i + ")" + ".lzw"; } split = CompressedFilePath.Split("compressions"); CompressedFilePath = split[0] + "compressions\\" + CompressedFileName; } byte[] nombre = Encoding.ASCII.GetBytes(originalName.PadRight(200, '\0').ToArray()); using (var fs = new FileStream(CompressedFilePath, FileMode.OpenOrCreate)) { fs.Write(nombre, 0, 200); fs.Seek(200, SeekOrigin.Begin); fs.Write(result, 0, result.Length); } CompressionRatio = lzw.CompressionRatio(); CompressionFactor = lzw.CompressionFactor(); ReductionPercentage = lzw.ReductionPercentage(); }
public void CompressFile(string path) { Huffman hf = new Huffman(); byte[] buffer; using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate)) { buffer = new byte[fs.Length]; using (var br = new BinaryReader(fs)) { br.Read(buffer, 0, (int)fs.Length); } } byte[] result = hf.EncodeData(buffer); for (int i = 1; File.Exists(CompressedFilePath); i++) { var split = CompressedFileName.Split(".huff"); if (split[0].Contains("(")) { var split2 = split[0].Split("("); CompressedFileName = split2[0] + "(" + i + ")" + ".huff"; } else { CompressedFileName = split[0] + "(" + i + ")" + ".huff"; } split = CompressedFilePath.Split("compressions"); CompressedFilePath = split[0] + "compressions\\" + CompressedFileName; } using (var fs = new FileStream(CompressedFilePath, FileMode.OpenOrCreate)) { fs.Write(result, 0, result.Length); } CompressionRatio = hf.CompressionRatio(); CompressionFactor = hf.CompressionFactor(); ReductionPercentage = hf.ReductionPercentage(); }