压缩/解压缩辅助类ZipUtil

/** <summary>
    /// 压缩/解压缩辅助类
    /// </summary>
    public sealed class ZipUtil
    {
        private ZipUtil()
        {
        }

        /** <summary>
        /// 压缩文件操作
        /// </summary>
        /// <param name="fileToZip">待压缩文件名</param>
        /// <param name="zipedFile">压缩后的文件名</param>
        /// <param name="compressionLevel">0~9,表示压缩的程度,9表示最高压缩</param>
        /// <param name="blockSize">缓冲块大小</param>
        public static void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize)
        {
            if (! File.Exists(fileToZip))
            {
                throw new FileNotFoundException("文件 " + fileToZip + " 没有找到,取消压缩。");
            }

            using (FileStream streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read))
            {
                FileStream fileStream = File.Create(zipedFile);
                using (ZipOutputStream zipStream = new ZipOutputStream(fileStream))
                {
                    ZipEntry zipEntry = new ZipEntry(fileToZip);
                    zipStream.PutNextEntry(zipEntry);
                    zipStream.SetLevel(compressionLevel);

                    byte[] buffer = new byte[blockSize];
                    Int32 size = streamToZip.Read(buffer, 0, buffer.Length);
                    zipStream.Write(buffer, 0, size);

                    try
                    {
                        while (size < streamToZip.Length)
                        {
                            int sizeRead = streamToZip.Read(buffer, 0, buffer.Length);
                            zipStream.Write(buffer, 0, sizeRead);
                            size += sizeRead;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    zipStream.Finish();
                }
            }
        }

        /** <summary>
        /// 打开sourceZipPath的压缩文件,解压到destPath目录中
        /// </summary>
        /// <param name="sourceZipPath">待解压的文件路径</param>
        /// <param name="destPath">解压后的文件路径</param>
        public static void UnZipFile(string sourceZipPath, string destPath)
        {
            if (!destPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                destPath = destPath + Path.DirectorySeparatorChar;
            }

            using (ZipInputStream zipInputStream = new ZipInputStream(File.OpenRead(sourceZipPath)))
            {
                ZipEntry theEntry;
                while ((theEntry = zipInputStream.GetNextEntry()) != null)
                {
                    string fileName = destPath + Path.GetDirectoryName(theEntry.Name) +
                        Path.DirectorySeparatorChar + Path.GetFileName(theEntry.Name);

                    //create directory for file (if necessary)
                    Directory.CreateDirectory(Path.GetDirectoryName(fileName));

                    if (!theEntry.IsDirectory)
                    {
                        using (FileStream streamWriter = File.Create(fileName))
                        {
                            int size = 2048;
                            byte[] data = new byte[size];
                            try
                            {
                                while (true)
                                {
                                    size = zipInputStream.Read(data, 0, data.Length);
                                    if (size > 0)
                                    {
                                        streamWriter.Write(data, 0, size);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                            }
                            catch
                            {
                            }
                        }
                    }
                }
            }
        }
    }
压缩/解压缩辅助类ZipUtil测试代码:
    public class TestZipUtil
    {
        public static string Execute()
        {
            string result = string.Empty;
            result += "使用ZipUtil压缩/解压缩辅助类:" + "/r/n";

            try
            {
                ZipUtil.ZipFile("Web.Config", "Test.Zip", 6, 512);
                ZipUtil.UnZipFile("Test.Zip", "Test");

                result += "操作完成!/r/n /r/n";
            }
            catch (Exception ex)
            {
                result += string.Format("发生错误:{0}!/r/n /r/n", ex.Message);
            }
            return result;
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值