C#压缩解压类

    public class FileCompressionHelper
    {
        /// <summary>  
        /// 压缩文件  
        /// </summary>  
        /// <param name="fileNames">要打包的文件列表</param>  
        /// <param name="GzipFileName">目标文件名</param>  
        /// <param name="CompressionLevel">压缩品质级别(0~9)</param>  
        /// <param name="SleepTimer">休眠时间(单位毫秒)</param> 
        /// <returns>是否成功</returns>
        public static bool Compress(List<FileInfo> fileNames, string GzipFileName, int CompressionLevel, int SleepTimer)
        {
            ZipOutputStream s = new ZipOutputStream(File.Create(GzipFileName));
            try
            {
                s.SetLevel(CompressionLevel);   //0 - store only to 9 - means best compression  
                foreach (FileInfo file in fileNames)
                {
                    FileStream fs = null;
                    try
                    {
                        fs = file.Open(FileMode.Open, FileAccess.ReadWrite);
                    }
                    catch
                    { continue; }
                    //  方法二,将文件分批读入缓冲区  
                    byte[] data = new byte[2048];
                    int size = 2048;
                    ZipEntry entry = new ZipEntry(Path.GetFileName(file.Name));
                    entry.DateTime = (file.CreationTime > file.LastWriteTime ? file.LastWriteTime : file.CreationTime);
                    s.PutNextEntry(entry);
                    while (true)
                    {
                        size = fs.Read(data, 0, size);
                        if (size <= 0) break;
                        s.Write(data, 0, size);
                    }
                    fs.Close();
                    file.Delete();
                    Thread.Sleep(SleepTimer);
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                s.Finish();
                s.Close();
            }
        }

        /// <summary>  
        /// 解压缩文件  
        /// </summary>  
        /// <param name="GzipFile">压缩包文件名</param>  
        /// <param name="targetPath">解压缩目标路径</param>    
        /// <returns>是否成功</returns>
        public static bool Decompress(string GzipFile, string targetPath)
        {
            //string directoryName = Path.GetDirectoryName(targetPath + "//") + "//";  
            string directoryName = targetPath;
            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);//生成解压目录  
            }
            string CurrentDirectory = directoryName;
            byte[] data = new byte[2048];
            int size = 2048;
            ZipEntry theEntry = null;
            try
            {
                using (ZipInputStream s = new ZipInputStream(File.OpenRead(GzipFile)))
                {
                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        if (theEntry.IsDirectory)
                        {// 该结点是目录  
                            if (!Directory.Exists(CurrentDirectory + theEntry.Name)) Directory.CreateDirectory(CurrentDirectory + theEntry.Name);
                        }
                        else
                        {
                            if (theEntry.Name != String.Empty)
                            {
                                //解压文件到指定的目录  
                                using (FileStream streamWriter = File.Create(CurrentDirectory + theEntry.Name))
                                {
                                    while (true)
                                    {
                                        size = s.Read(data, 0, data.Length);
                                        if (size <= 0) break;

                                        streamWriter.Write(data, 0, size);
                                    }
                                    streamWriter.Close();
                                }
                            }
                        }
                    }
                    s.Close();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值