通过SharpZipLib来压缩解压文件

时间:2021-04-30 16:28:13

在项目开发中,一些比较常用的功能就是压缩解压文件了,其实类似的方法有许多 ,现将通过第三方类库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();
        }
    }