zoukankan      html  css  js  c++  java
  • ICSharpCode.SharpZipLib 内存压缩 下载

        private void DownLoadAsZip(string folderName, string outFileName)
        {
            //取文件夹下文件,只取当前目录下文件,子文件不取
            string[] files = Directory.GetFiles(folderName);
    
            int folderOffset = folderName.Length + (folderName.EndsWith("\") ? 0 : 1);  //设置文件路径长度,方便后续取文件夹中文件及生成子文件夹
    
            MemoryStream outputMemStream = new MemoryStream();
            ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);
            zipStream.SetLevel(0);
    
            foreach (string filename in files)
            {
    
                FileInfo fi = new FileInfo(filename);
    
                string entryName = filename.Substring(folderOffset); // Makes the name in zip based on the folder //取文件名称
                entryName = ZipEntry.CleanName(entryName); // Removes drive from name and fixes slash direction//格式化文件名称
                ZipEntry newEntry = new ZipEntry(entryName); //生成压缩文件项
                newEntry.DateTime = fi.LastWriteTime; // Note the zip format stores 2 second granularity
    
                newEntry.Size = fi.Length;
    
                zipStream.PutNextEntry(newEntry); //把压缩文件项加入 压缩文件流,应该是打开项目添加接口
    
                // Zip the file in buffered chunks
                // the "using" will close the stream even if an exception occurs
                byte[] buffer = new byte[4096];
                using (FileStream streamReader = File.OpenRead(filename))
                {
                    StreamUtils.Copy(streamReader, zipStream, buffer); //把压缩文件项的文件流 copy 进 压缩文件流
                }
                zipStream.CloseEntry();
            }
    
            zipStream.IsStreamOwner = false;    // False stops the Close also Closing the underlying stream.
            zipStream.Close();          // Must finish the ZipOutputStream before using outputMemStream.
    
            //导出
    
            Response.ContentType = "application/zip";
            Response.AddHeader("content-disposition", "attachment;  filename=" + HttpUtility.UrlEncode(outFileName + ".zip", System.Text.Encoding.UTF8));
            outputMemStream.WriteTo(Response.OutputStream);
            Response.Flush();
            Response.End();
    
        }
  • 相关阅读:
    java HTTP代码示例
    eclipse创建文件package,source folder和folder区别及相互转换
    spring 及 spring boot 资源文件配置
    深入理解Java枚举类型(enum)
    Linux环境变量配置的三个方法--/etc/profile,~/.bashrc,shell
    JAXB和XStream比较
    java将配置信息写在数据库(利用反射)
    【大数据实战】Logstash采集->Kafka->ElasticSearch检索
    Linux 安装rabbitmq 遇到的一些问题
    Linux 下的jdk安装
  • 原文地址:https://www.cnblogs.com/BinBinGo/p/11206089.html
Copyright © 2011-2022 走看看