• 使用 ICSharpCode.SharpZipLib.Zip压缩解压文件


            /// <summary>  
            /// 功能:解压zip格式的文件。  
            /// </summary>  
            /// <param name="zipFilePath">压缩文件路径</param>  
            /// <param name="unZipDir">解压文件存放路径,为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹</param>  
            /// <returns>解压是否成功</returns>  
            public static bool UnZip(string zipFilePath, string unZipDir)
            {
                try
                {
                    if (zipFilePath == string.Empty)
                    {
                        throw new Exception("压缩文件不能为空!");
                    }
                    if (!File.Exists(zipFilePath))
                    {
                        throw new FileNotFoundException("压缩文件不存在!");
                    }
                    //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹  
                    if (unZipDir == string.Empty)
                        unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));
                    if (!unZipDir.EndsWith("/"))
                        unZipDir += "/";
                    if (!Directory.Exists(unZipDir))
                        Directory.CreateDirectory(unZipDir);
                    using (var s = new ZipInputStream(File.OpenRead(zipFilePath)))
                    {
    
                        ZipEntry theEntry;
                        while ((theEntry = s.GetNextEntry()) != null)
                        {
                            string directoryName = Path.GetDirectoryName(theEntry.Name);
                            string fileName = Path.GetFileName(theEntry.Name);
                            if (!string.IsNullOrEmpty(directoryName))
                            {
                                Directory.CreateDirectory(unZipDir + directoryName);
                            }
                            if (directoryName != null && !directoryName.EndsWith("/"))
                            {
                            }
                            if (fileName != String.Empty)
                            {
                                using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))
                                {
    
                                    int size;
                                    byte[] data = new byte[2048];
                                    while (true)
                                    {
                                        size = s.Read(data, 0, data.Length);
                                        if (size > 0)
                                        {
                                            streamWriter.Write(data, 0, size);
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    return true;
                }
                catch (Exception)
                {
    
                    return false;
                }
    
            }
    
            /// <summary>
            /// 压缩所有的文件
            /// </summary>
            /// <param name="filesPath"></param>
            /// <param name="zipFilePath"></param>
            public static void CreateZipFile(string filesPath, string zipFilePath)
            {
                if (!Directory.Exists(filesPath))
                {
                    return;
                }
                ZipOutputStream stream = new ZipOutputStream(File.Create(zipFilePath));
                stream.SetLevel(0); // 压缩级别 0-9
                byte[] buffer = new byte[4096]; //缓冲区大小
                string[] filenames = Directory.GetFiles(filesPath, "*.*", SearchOption.AllDirectories);
                foreach (string file in filenames)
                {
                    ZipEntry entry = new ZipEntry(file.Replace(filesPath, ""));
                    entry.DateTime = DateTime.Now;
                    stream.PutNextEntry(entry);
                    using (FileStream fs = File.OpenRead(file))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            stream.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                    }
                }
                stream.Finish();
                stream.Close();
            }

    里面是参考网友的,使用比较顺手。

  • 相关阅读:
    JS获取当前网页大小以及屏幕分辨率等
    JS获取浏览器信息及屏幕分辨率
    vue中获取客户端IP地址(不需要额外引入三方文件)
    vue事件修饰器
    export,import ,export default的区别
    Vue中动态添加多个class
    Vue中通过鼠标移入移出来添加或取消class样式(active)
    restapi(5)- rest-mongo 应用实例:分布式图片管理系统之一,rest 服务
    restapi(4)- rest-mongo : MongoDB数据库前端的httpserver
    restapi(3)- MongoDBEngine : MongoDB Scala编程工具库
  • 原文地址:https://www.cnblogs.com/hbh123/p/9566334.html
Copyright © 2020-2023  润新知