zoukankan      html  css  js  c++  java
  • C#文件压缩成.Zip

    使用的三方类库ICSharpCode.SharpZipLib.dll

    方法如下:

     /// <summary>
            /// 压缩文件为zip格式
            /// </summary>
            /// <param name="filepath">文件地址</param>
            /// <param name="targetPath">目标路径</param>
            static bool CompressFiles(string filePath, string targetPath)
            {
                try
                {
                    using (ZipOutputStream zipstream = new ZipOutputStream(File.Create(targetPath)))
                    {
                        zipstream.SetLevel(9); // 压缩级别 0-9
                        byte[] buffer = new byte[4096]; //缓冲区大小
                        ZipEntry entry = new ZipEntry(Path.GetFileName(filePath));
                        entry.DateTime = DateTime.Now;
                        zipstream.PutNextEntry(entry);
                        using (FileStream fs = File.OpenRead(filePath))
                        {
                            int sourceBytes;
                            do
                            {
                                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                                zipstream.Write(buffer, 0, sourceBytes);
                            }
                            while (sourceBytes > 0);
                        }
                        zipstream.Finish();
                        zipstream.Close();
                        return true;
                    }
                }
                catch (Exception ex)
                {                
                    return false;
                }
            }

     压缩文件夹

          /// <summary>
            /// 压缩文件夹
            /// </summary>
            /// <param name="strFile"></param>
            /// <param name="strZip"></param>
            /// <returns></returns>
            public static bool ZipFile(string strFile, string strZip)
            {
                try
                {
                    if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
                    {
                        strFile += Path.DirectorySeparatorChar;
                    }
                    ZipOutputStream outstream = new ZipOutputStream(File.Create(strZip));
                    outstream.SetLevel(6);
                    ZipCompress(strFile, outstream, strFile);
                    outstream.Finish();
                    outstream.Close();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
    
            public static void ZipCompress(string strFile, ZipOutputStream outstream, string staticFile)
            {
                if (strFile[strFile.Length - 1] != Path.DirectorySeparatorChar)
                {
                    strFile += Path.DirectorySeparatorChar;
                }
                Crc32 crc = new Crc32();
                //获取指定目录下所有文件和子目录文件名称
                string[] filenames = Directory.GetFileSystemEntries(strFile);
                //遍历文件
                foreach (string file in filenames)
                {
                    if (Directory.Exists(file))
                    {
                        ZipCompress(file, outstream, staticFile);
                    }
                    //否则,直接压缩文件
                    else
                    {
                        //打开文件
                        FileStream fs = File.OpenRead(file);
                        //定义缓存区对象
                        byte[] buffer = new byte[fs.Length];
                        //通过字符流,读取文件
                        fs.Read(buffer, 0, buffer.Length);
                        //得到目录下的文件(比如:D:Debug1	est),test
                        string tempfile = file.Substring(staticFile.LastIndexOf("\") + 1);
                        ZipEntry entry = new ZipEntry(tempfile);
                        entry.DateTime = DateTime.Now;
                        entry.Size = fs.Length;
                        fs.Close();
                        crc.Reset();
                        crc.Update(buffer);
                        entry.Crc = crc.Value;
                        outstream.PutNextEntry(entry);
                        //写文件
                        outstream.Write(buffer, 0, buffer.Length);
                    }
                }
            }
  • 相关阅读:
    php jquery pjax示例源码 (ajax请求,并改变url)
    mysql 中查看指定表的字段名 (可根据字段变量生成c#后台代码)
    原生js Ajax
    ajax basic 认证
    Json序列化问题
    MSSQL 日期操作函数 总结
    用sql语句按周、按月、按季、按年统
    mssql中得到当天数据的语句
    SP_CreateInsertScript 将表内的数据全部拼接成INSERT字符串输出
    MSSQL 获取指定日期所在星期的第一天和最后一天日期 获取指定日期坐在月的第一天和最后一天
  • 原文地址:https://www.cnblogs.com/cdjbolg/p/11835835.html
Copyright © 2011-2022 走看看