/// <summary> /// 对文件和字符串压缩及解压缩类 /// </summary> public class GZip { /// <summary> /// 对字符串进行压缩 /// </summary> /// <param name="str">待压缩的字符串</param> /// <returns>压缩后的字符串</returns> public static string CompressString(string str) { string compressString = ""; byte[] compressBeforeByte = Encoding.Default.GetBytes(str); byte[] compressAfterByte=GZip.Compress(compressBeforeByte); compressString = Encoding.Default.GetString(compressAfterByte); return compressString; } /// <summary> /// 对字符串进行解压缩 /// </summary> /// <param name="str">待解压缩的字符串</param> /// <returns>解压缩后的字符串</returns> public static string DecompressString(string str) { string compressString = ""; byte[] compressBeforeByte = Encoding.Default.GetBytes(str); byte[] compressAfterByte = GZip.Decompress(compressBeforeByte); compressString = Encoding.Default.GetString(compressAfterByte); return compressString; } /// <summary> /// 对文件进行压缩 /// </summary> /// <param name="sourceFile">待压缩的文件名</param> /// <param name="destinationFile">压缩后的文件名</param> public static void CompressFile(string sourceFile, string destinationFile) { throw new Exception("The method or operation is not implemented."); } /// <summary> /// 对文件进行解压缩 /// </summary> /// <param name="sourceFile">待解压缩的文件名</param> /// <param name="destinationFile">解压缩后的文件名</param> /// <returns></returns> public static void DecompressFile(string sourceFile, string destinationFile) { throw new Exception("The method or operation is not implemented."); } /// <summary> /// 对byte数组进行压缩 /// </summary> /// <param name="data">待压缩的byte数组</param> /// <returns>压缩后的byte数组</returns> public static byte[] Compress(byte[] data) { try { MemoryStream ms = new MemoryStream(); GZipStream zip = new GZipStream(ms, CompressionMode.Compress,true); zip.Write(data, 0, data.Length); zip.Close(); byte[] buffer = new byte[ms.Length]; ms.Position=0; ms.Read(buffer, 0, buffer.Length); ms.Close(); return buffer; } catch (Exception e) { throw new Exception(e.Message); } } public static byte[] Decompress(byte[] data) { try { MemoryStream ms = new MemoryStream(data); GZipStream zip = new GZipStream(ms,CompressionMode.Decompress,true); MemoryStream msreader = new MemoryStream(); byte[] buffer = new byte[0x1000]; while (true) { int reader=zip.Read(buffer, 0, buffer.Length); if (reader <= 0) { break; } msreader.Write(buffer, 0, reader); } zip.Close(); ms.Close(); msreader.Position = 0; buffer = msreader.ToArray(); msreader.Close(); return buffer; } catch (Exception e) { throw new Exception(e.Message); } } //// <summary> /// 对目标文件夹进行压缩,将压缩结果保存为指定文件 /// </summary> /// <param name="dirPath">目标文件夹</param> /// <param name="fileName">压缩文件</param> public static void Compress(string dirPath, string fileName) { ArrayList list = new ArrayList(); foreach (string f in Directory.GetFiles(dirPath)) { byte[] destBuffer = File.ReadAllBytes(f); SerializeFileInfo sfi = new SerializeFileInfo(f, destBuffer); list.Add(sfi); } IFormatter formatter = new BinaryFormatter(); using (Stream s = new MemoryStream()) { formatter.Serialize(s, list); s.Position = 0; CreateCompressFile(s, fileName); } } //// <summary> /// 对目标压缩文件解压缩,将内容解压缩到指定文件夹 /// </summary> /// <param name="fileName">压缩文件</param> /// <param name="dirPath">解压缩目录</param> public static void DeCompress(string fileName, string dirPath) { using (Stream source = File.OpenRead(fileName)) { using (Stream destination = new MemoryStream()) { using (GZipStream input = new GZipStream(source, CompressionMode.Decompress, true)) { byte[] bytes = new byte[4096]; int n; while ((n = input.Read(bytes, 0, bytes.Length)) != 0) { destination.Write(bytes, 0, n); } } destination.Flush(); destination.Position = 0; DeSerializeFiles(destination, dirPath); } } } private static void DeSerializeFiles(Stream s, string dirPath) { BinaryFormatter b = new BinaryFormatter(); ArrayList list = (ArrayList)b.Deserialize(s); foreach (SerializeFileInfo f in list) { string newName = dirPath + Path.GetFileName(f.FileName); using (FileStream fs = new FileStream(newName, FileMode.Create, FileAccess.Write)) { fs.Write(f.FileBuffer, 0, f.FileBuffer.Length); fs.Close(); } } } private static void CreateCompressFile(Stream source, string destinationName) { using (Stream destination = new FileStream(destinationName, FileMode.Create, FileAccess.Write)) { using (GZipStream output = new GZipStream(destination, CompressionMode.Compress)) { byte[] bytes = new byte[4096]; int n; while ((n = source.Read(bytes, 0, bytes.Length)) != 0) { output.Write(bytes, 0, n); } } } } [Serializable] class SerializeFileInfo { public SerializeFileInfo(string name, byte[] buffer) { fileName = name; fileBuffer = buffer; } string fileName; public string FileName { get { return fileName; } } byte[] fileBuffer; public byte[] FileBuffer { get { return fileBuffer; } } } }
相关推荐
该课程设计可以实现对于英文文本进行字符串压缩解压处理,附带课程设计报告、源代码、运行结果等。
Java实现压缩与解压缩ZIP import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream;...
CompresszZipFile 中带是 zip解压 ZipFileUtil 是文件的压缩 和rar的解压
CompressStringUtil压缩字符串与解压缩字符串 DESUtil加解密 DigestUtil加解密 Dimension2CodeUtil二维码生成与解析 Md5Util加解密 Pinyin4jUtil中文转拼音、中文转拼音首字母、中文转字节序列 PwdUtils生成4-32位的...
3 编码:根据编码表对输入的字符串进行编码压缩 并将编码后的字符串输出到compress txt文件中 4 译码:对compress txt中的压缩字符进行解压缩 把解压的答案输出到文件decompress txt文件中 5 比较decompress txt和...
使用哈夫曼编码实现对文本文件的压缩和解压缩
Oracle P/L SQL实现文件压缩、解压功能,以下是此过程包的头部,包体经常打包处理plb,感兴趣用户可以下载下来。 Create or Replace Package UTL_ZIP AUTHID CURRENT_USER as Type File_List is Table of Clob; -...
建立相应的huffman树 对上述字符进行编码 3 编码:根据编码表对输入的字符串进行编码压缩 并将编码后的字符串输出到compress txt文件中 4 译码:对compress txt中的压缩字符进行解压缩 把解压的答案输出到文件...
蓝精灵(文件打包、压缩、AES加密、字符串搜索、彻底删除) 这是一款小巧的文件管理程序,基本功能是文件及子目录打包、无损压缩、加密,没有复杂的文档管理,满足一些用户的需要。当今是信息时代,需要的是效率...
十六进制压缩Hexcompress不是通用的压缩算法。 相反,hexcompress 非常擅长压缩包含许多数字或十六... 解压缩数据"1337"只会解压为字符串"1337" ,这使得它很容易与可能压缩或可能尚未压缩的新代码集成。 (这个事实在
java实现霍夫曼(huffman)树的压缩和解压缩,支持对文档的压缩和解压缩
前端pako压缩与解压缩
python中zlib模块是用来压缩或者解压缩数据,以便保存和传输。它是其他压缩工具的基础。下面来一起看看python用模块zlib压缩与解压字符串和文件的方法。话不多说,直接来看示例代码。 例子1:压缩与解压字符串 ...
字符串-Java解题分析-学习资料.zip 是一个关于Java中字符串处理的解题分析和学习资料的压缩文件。该资源主要涵盖了Java中字符串的基本操作、常见算法和问题解析,旨在帮助开发者深入理解字符串在Java中的运用,提高...
python版本为2.7.9,大家注意别下错了,里面有一个txt文件是进行压缩的,可以更改文件中的变量path1来对其他文件进行压缩与解压,代码中有详细注释,实现过程虽然简单,但是包含自己很多一些独特的想法,自己的知识...
通过读取TXT文件,输出压缩后的TXZ文件,现实压缩率,提供解压缩;
- 以xx 为文件名的文件通过tar 和gzip 打包压缩产生,该文件中以字符串的方式记录 了一个非负整数; - 或者以xx 为名的目录通过tar 和gzip 打包压缩产生,该目录中包含若干xx.tar.gz。 其中,x 2 [0, 9]。 现...
问题:将一张bmp图像的灰度值... 位图文件类型,必须是0x424D,即字符串“BM” DWORD bfSize; 位图文件大小,包括这14个字节....此篇文章详细的介绍了图像解压缩与压缩算法,附有源代码且带有注释,希望能够提供帮助
通过json格式传入字符串实现多文件或目录的zip压缩或解压缩。详情阅读readme.txt
C#利用SharpZipLib进行文件压缩和解压实例