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(); }
public void WriteCompression(FileManage file) { string[] path = CompressedFilePath.Split("Data"); string file_path = path[0] + "\\Data\\compressions_history.json"; List <FileManage> list; string json_text = ""; using (FileStream fs = new FileStream(file_path, FileMode.OpenOrCreate)) { using (StreamReader sr = new StreamReader(fs)) { json_text = sr.ReadToEnd(); } } if (json_text == "") { using (StreamWriter sw = new StreamWriter(file_path, false, Encoding.UTF8)) { string file_stats = JsonSerializer.Serialize(file); sw.Write("[" + file_stats + "]"); return; } } else { JsonSerializerOptions name_rule = new JsonSerializerOptions { IgnoreNullValues = true }; list = JsonSerializer.Deserialize <List <FileManage> >(json_text, name_rule); list.Add(file); } using (StreamWriter sw = new StreamWriter(file_path, false, Encoding.UTF8)) { string file_stats = JsonSerializer.Serialize(list); sw.Write(file_stats); } }