对文件和字符串压缩及解压缩类

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->    /// <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,data.Length);
                zip.Close();
                byte[] buffer = new byte[ms.Length];
                ms.Position=0;
                ms.Read(buffer,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,buffer.Length);
                    if (reader <= 0)
                    {
                        break;
                    }
                    msreader.Write(buffer,reader);                   
                }
                zip.Close();
                ms.Close();
                msreader.Position = 0;
                buffer = msreader.ToArray();
                msreader.Close();
                return buffer;
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
代码Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->/**//// <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,true))
                    {
                        byte[] bytes = new byte[4096];
                        int n;
                        while ((n = input.Read(bytes,bytes.Length)) != 0)
                        {
                            destination.Write(bytes,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,f.filebuffer.Length);
                    fs.Close();
                }
            }
        }

        private static void CreateCompressFile(Stream source,string destinationName)
        {
            using (Stream destination = new FileStream(destinationName,FileAccess.Write))
            {
                using (GZipStream output = new GZipStream(destination,CompressionMode.Compress))
                {
                    byte[] bytes = new byte[4096];
                    int n;
                    while ((n = source.Read(bytes,bytes.Length)) != 0)
                    {
                        output.Write(bytes,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;
                }
            }
        }
    }
From:http://www.cnblogs.com/chenqingwei/archive/2010/07/15/1777806.html

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...