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

    使用SharpZipLib插件   插件地址:http://icsharpcode.github.io/SharpZipLib/

    using ICSharpCode.SharpZipLib.Zip;
    
     public static void BatchDownFile(List<string> urlList)
            {
                /*删除之前的临时文件*/
                string path = Path.Combine(privateDiskBasePath,"tempZip");
                DirectoryInfo dirInfo = new DirectoryInfo(path);
                if (dirInfo.Exists)
                {
                    dirInfo.Delete(true);
                }
                dirInfo.Create();
                string fileName = Path.Combine(path, "打包文件.zip");
                using (ZipFile zip = ZipFile.Create(fileName))
                {
                    zip.BeginUpdate();
                    zip.SetComment("压缩包");
    
                    foreach (var item in urlList)
                    {
                        if (File.Exists(item))//判断是文件还是文件夹
                        {
                            byte[] buffer = null;
                            try
                            {
    
                                buffer = File.ReadAllBytes(item);
                            }
                            catch
                            {
                            }
    
                            if (buffer != null && buffer.Length > 0)
                            {
                                StreamDataSource dataSource = new StreamDataSource(buffer);
                                string[] fileNameSplit = item.Split('\');
    
                                zip.Add(dataSource, fileNameSplit[fileNameSplit.Length - 1]);
                            }
                        }
                    }
                    zip.CommitUpdate();
                }
                DownFile("打包文件.zip", fileName);
            }
     public class StreamDataSource : IStaticDataSource
        {
            public byte[] Buffer { get; private set; }
            public StreamDataSource(byte[] buffer)
            {
                Buffer = buffer;
            }
            public Stream GetSource()
            {
                return new MemoryStream(Buffer);
            }
        }
    public static void BatchDownFile(List<string> urlList, int userId)
            {
                string privateDiskBasePath = string.Empty;
                /*删除之前的临时文件*/
                string path = Path.Combine(privateDiskBasePath, userId.ToString(), "tempZip");
                DirectoryInfo dirInfo = new DirectoryInfo(path);
                if (dirInfo.Exists)
                {
                    dirInfo.Delete(true);
                }
                dirInfo.Create();
                string zipFileName = Path.Combine(path, "打包文件.zip");
                using (FileStream fs = File.Create(zipFileName))
                {
                    using (ZipOutputStream zipStream = new ZipOutputStream(fs))
                    {
                        foreach (var item in urlList)
                        {
                            using (FileStream toZipFs = new FileStream(item, FileMode.Open, FileAccess.Read))
                            {
                                string fileName = item.Substring(item.LastIndexOf("\") + 1);
                                ZipEntry zipEntry = new ZipEntry(fileName);
                                zipStream.PutNextEntry(zipEntry);
                                zipStream.SetLevel(5);//设置压缩等级
    
                                byte[] buffer = new byte[2048];
    
                                int sizeRead = 0;
                                try
                                {
                                    do
                                    {
                                        sizeRead = toZipFs.Read(buffer, 0, buffer.Length);
                                        zipStream.Write(buffer,0, sizeRead);
                                    } while (sizeRead > 0);
                                }
                                catch (Exception)
                                {
                                    throw;
                                }
                                toZipFs.Close();
                            }
                        }
                        zipStream.Finish();
                        zipStream.Close();
                    }
    
                    fs.Close();
                }
    
            }
  • 相关阅读:
    leetcode 5414 收藏清单
    leetcode 714 买卖股票的最佳时机含手续费(dp)
    春招的一个记录
    leetcode 560 和为k的子数组(前缀和 + map)
    机器学习--激活函数篇
    leetcode 回文链表(O(1)空间复杂度)
    Leetcode 659 分割数组为连续子序列 (贪心)
    论文笔记:MeLU: Meta-Learned User Preference Estimator for Cold-Start Recommendation
    jni.h头文件详解一
    JNI笔记
  • 原文地址:https://www.cnblogs.com/tangchun/p/7858344.html
Copyright © 2011-2022 走看看