• 通过SharpZipLib来压缩解压文件


    在项目开发中,一些比较常用的功能就是压缩解压文件了,其实类似的方法有许多 ,现将通过第三方类库SharpZipLib来压缩解压文件的方法介绍如下,主要目的是方便以后自己阅读,当然可以帮到有需要的朋友更好了,代码比较简单。

    public class ZipAndUnzipFileBySharpZipLib
        {
            /// <summary>
            /// 解压文件
            /// </summary>
            /// <param name="SrcFile"></param>
            /// <param name="DstFolder"></param>
            public static void UnZip(string SrcFile, string DstFolder)
            {
                FileStream fileStreamIn = new FileStream(SrcFile, FileMode.Open, FileAccess.Read);
                ZipInputStream zipInStream = new ZipInputStream(fileStreamIn);
                ZipEntry entry = zipInStream.GetNextEntry();
                while (entry != null)
                {
                    FileStream fileStreamOut = new FileStream(DstFolder + @"" + entry.Name, FileMode.Create, FileAccess.Write);
                    int size;
                    byte[] buffer = new byte[1024];
                    do
                    {
                        size = zipInStream.Read(buffer, 0, buffer.Length);
                        fileStreamOut.Write(buffer, 0, size);
                    }
                    while (size > 0);
                    fileStreamOut.Close();

                    entry = zipInStream.GetNextEntry();
                }
                zipInStream.Close();
                fileStreamIn.Close();
            }

            /// <summary>
            /// 压缩文件或目录
            /// </summary>
            /// <param name="srcPath"></param>
            /// <param name="dstFile"></param>
            public static void Zip(string srcPath, string dstFile)
            {
                bool isFile = false;
                bool isFolder = false;
                if(File.Exists(srcPath))
                {
                    isFile = true;
                }
                if(Directory.Exists(srcPath))
                {
                    isFolder = true;
                }
                if (isFile == false && isFolder == false) { return; }
                FileStream fileStreamOut = new FileStream(dstFile, FileMode.Create, FileAccess.Write);
                ZipOutputStream zipOutputStream = new ZipOutputStream(fileStreamOut);
                if(isFile)
                {
                    ZipFile(srcPath,zipOutputStream);
                }
                if(isFolder)
                {
                   IEnumerable<string> files = Directory.EnumerateFiles(srcPath);
                   foreach (var file in files)
                   {
                       ZipFile(file, zipOutputStream);
                   }
                }
                zipOutputStream.Close();
                fileStreamOut.Close();
            }

            /// <summary>
            /// 压缩文件
            /// </summary>
            /// <param name="srcFile">待压缩的文件路径</param>
            /// <param name="zipOutputStream">压缩stream</param>

            private static void ZipFile(string srcFile,ZipOutputStream zipOutputStream)
            {
                FileStream fileStreamIn = new FileStream(srcFile, FileMode.Open, FileAccess.Read);
                byte[] buffer = new byte[1024];
                ZipEntry entry = new ZipEntry(Path.GetFileName(srcFile));
                zipOutputStream.PutNextEntry(entry);
                int size;
                do
                {
                    size = fileStreamIn.Read(buffer, 0, buffer.Length);
                    zipOutputStream.Write(buffer, 0, size);
                } while (size > 0);
                fileStreamIn.Close();
            }
        }

  • 相关阅读:
    学习CSS定位(布局)
    XSLT
    CSS模型框学习笔记
    XMLDOM复习
    XMLDOM之浏览器差异
    HTMLDOM入门
    学习二级纵向菜单两步走
    Xpath很全的学习地点,不看后悔
    LinQ To XML
    收集的一些零散代码
  • 原文地址:https://www.cnblogs.com/JustYong/p/3867696.html
Copyright © 2020-2023  润新知