public void AssertCompressAndUnCompressFileStream(string saveFileName, Dictionary <string, FileStream> fileStreams, byte compressLevelFlag) { var compressStream = new FileStream(saveFileName + ".multifile.huf", FileMode.Create); compressStream.Seek(0, SeekOrigin.Begin); var cw = new CompressStreamWriter(compressStream); cw.Write(fileStreams, compressLevelFlag, (int)EncryptType.None); compressStream.Seek(0, SeekOrigin.Begin); // 解压缩 using (var cr = new CompressStreamReader(compressStream)) { cr.Read(null, s => File.Create("recover." + s)); } foreach (var s in fileStreams.Keys) { using (var fs = File.OpenRead("recover." + s)) { using (var br = new BinaryReader(fs)) { fileStreams[s].Seek(0, SeekOrigin.Begin); using (var br2 = new BinaryReader(fileStreams[s])) { // TODO 长度有可能会溢出,不过这是测试,一般不用管 Assert.AreEqual(BitConverter.ToString(br2.ReadBytes((int)br2.BaseStream.Length)), BitConverter.ToString(br.ReadBytes((int)br.BaseStream.Length))); } } } } }
private void UnCompress_Button_Click(object sender, RoutedEventArgs e) { var encoding = new UTF8Encoding(); try { using (var unCompressStream = new MemoryStream()) { using (var compressStream = new MemoryStream(Convert.FromBase64String(TextboxCompressed.Text))) { using (var cr = new CompressStreamReader(compressStream)) { var readBytesCount = cr.Read(() => unCompressStream); unCompressStream.Seek(0, SeekOrigin.Begin); // 修复 Windows 7 下输出不可打印字符的问题 var bytes = new byte[readBytesCount]; Array.Copy(unCompressStream.GetBuffer(), bytes, readBytesCount); TextboxUncompressed.Text = encoding.GetString(bytes); } } } } catch (Exception ex) { MessageBox.Show(ex.Message, "哎呀,发生了一个错误~", MessageBoxButton.OK, MessageBoxImage.Error); } }
public void AssertCompressAndUnCompressBytes(string saveFileName, byte[] sourceData, byte compressLevelFlag) { var output = new MemoryStream(); var input = new MemoryStream(sourceData); var compressStream = new FileStream(saveFileName + ".test.huf", FileMode.Create); compressStream.Seek(0, SeekOrigin.Begin); var cw = new CompressStreamWriter(compressStream); cw.Write(input, compressLevelFlag, (int)EncryptType.None); compressStream.Seek(0, SeekOrigin.Begin); var cr = new CompressStreamReader(compressStream); cr.Read(() => output); output.Seek(0, SeekOrigin.Begin); var br = new BinaryReader(output); Assert.AreEqual(BitConverter.ToString(sourceData), BitConverter.ToString(br.ReadBytes(sourceData.Length))); }
private void UnCompress_Files_Button_Click(object sender, RoutedEventArgs e) { try { // 选择要压缩的文件 var fileOpenDialog = new OpenFileDialog { Multiselect = false, Filter = "HUF压缩文件|*.huf", Title = "选择你要解压缩的文件", CheckFileExists = true }; fileOpenDialog.FileOk += (o2, e2) => { var folderBrowserDialog = new FolderBrowserDialog { Description = "选择你要解压到的文件夹" }; if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { using (var cr = new CompressStreamReader(((OpenFileDialog)o2).OpenFile())) { cr.Read(null, s => File.Create(Path.Combine(folderBrowserDialog.SelectedPath, s))); } MessageBox.Show("解压成功!", "信息", MessageBoxButton.OK, MessageBoxImage.Information); } }; MessageBox.Show("解压过大的文件可能会导致程序暂时失去响应,建议使用不大于 50 kb 的 HUF 压缩文件解压缩测试", "警告", MessageBoxButton.OK, MessageBoxImage.Warning); fileOpenDialog.ShowDialog(); } catch (Exception exception) { MessageBox.Show(exception.Message, "哎呀,发生了一个错误~", MessageBoxButton.OK, MessageBoxImage.Error); } }