ICSharpCode.SharpZipLib

作者:追风剑情 发布于:2017-8-30 11:28 分类:C#

using System;
using System.IO;
using System.Collections.Generic;
using ICSharpCode.SharpZipLib.Zip;

namespace SharpZipLibTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string cur_path = Directory.GetCurrentDirectory();
            string res_path = Directory.GetCurrentDirectory() + "/res";
            string zip_path = Directory.GetCurrentDirectory() + "/res.zip";
            string unzip_path = Directory.GetCurrentDirectory() + "/unzip_res";

            Console.WriteLine("要压缩的资源目录: " + res_path);
            Console.WriteLine("压缩后的文件名: "+zip_path);

            ZipHelp.Compress(res_path, zip_path);
            ZipHelp.UnCompress(zip_path, unzip_path);

            Console.WriteLine("完成!");
            Console.Read();
        }
    }

    /// <summary>
    /// Zip辅助类
    /// </summary>
    public class ZipHelp
    {
        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="fileOrDir">要压缩的文件或文件夹路径</param>
        /// <param name="savePath">要保存的文件名</param>
        public static void Compress(string fileOrDir, string savePath)
        {
            if (Path.HasExtension(fileOrDir))
                CompressFile(fileOrDir, savePath);
            else
                CompressDirectory(fileOrDir, savePath);
        }

        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="zipPath">zip文件所在路径</param>
        /// <param name="savePath">解压后要存放的目录</param>
        public static void UnCompress(string zipPath, string savePath)
        {
            if (!File.Exists(zipPath))
            {
                throw new FileNotFoundException(zipPath);
            }

            FileStream fs = File.OpenRead(zipPath);
            ZipInputStream zis = new ZipInputStream(fs);
            ZipEntry entry = zis.GetNextEntry();
            while (entry != null)
            {
                string filename = savePath + Path.DirectorySeparatorChar + entry.Name;
                string dir = Path.GetDirectoryName(filename);
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);

                if (!entry.IsDirectory)
                    WriteFile(filename, zis);

                zis.CloseEntry();
                entry = zis.GetNextEntry();
            }
            zis.Close();
        }

        private static void CompressFile(string filePath, string savePath)
        {
            if (!File.Exists(filePath))
            {
                throw new FileNotFoundException(filePath);
            }

            ZipOutputStream zs = new ZipOutputStream(File.Create(savePath));
            ZipEntry entry = new ZipEntry(filePath);
            entry.DateTime = DateTime.Now;
            zs.PutNextEntry(entry);
            zs.Close();
        }

        private static void CompressDirectory(string dir, string savePath)
        {
            if (!Directory.Exists(dir))
            {
                throw new DirectoryNotFoundException(dir);
            }

            string parent_dir = Directory.GetParent(dir).FullName;

            ZipOutputStream zs = new ZipOutputStream(File.Create(savePath));
            dir_list.Clear();
            dir_list.Add(dir);
            SearchDirectory(dir);

            //遍历所有文件夹
            for (int i = 0; i < dir_list.Count; i++)
            {
                string path = dir_list[i];
                DirectoryInfo TheFolder = new DirectoryInfo(path);
                //遍历文件夹下的所有文件
                foreach (FileInfo NextFile in TheFolder.GetFiles())
                {
                    if (NextFile.Extension.Equals(".zip"))
                        continue;
                    FileStream fs = null;
                    try
                    {
                        fs = NextFile.Open(FileMode.Open, FileAccess.ReadWrite);
                    }
                    catch
                    {
                        continue;
                    }
                    //将文件分批读入缓存中
                    byte[] data = new byte[2048];
                    int size = 2048;
                    ZipEntry entry = new ZipEntry(Path.GetFileName(NextFile.Name));
                    entry.DateTime = DateTime.Now;
                    zs.PutNextEntry(entry);
                    while (true)
                    {
                        size = fs.Read(data, 0, size);
                        if (size <= 0) break;
                        zs.Write(data, 0, size);
                    }
                    fs.Close();
                }
            }
            zs.Finish();
            zs.Close();
        }

        private static List<string> dir_list = new List<string>();

        private static void SearchDirectory(string dir)
        {
            DirectoryInfo TheFolder = new DirectoryInfo(dir);
            foreach (DirectoryInfo NextFolder in TheFolder.GetDirectories())
            {
                dir_list.Add(NextFolder.FullName);
                SearchDirectory(NextFolder.FullName);
            }
        }

        private static void WriteFile(string saveFile, ZipInputStream zis)
        {
            //创建目录
            string dir = Path.GetDirectoryName(saveFile);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);

            byte[] buffer = new byte[4 * 1024];
            FileStream fs = File.Create(saveFile);
            int len = 0;
            while ((len = zis.Read(buffer, 0, buffer.Length)) > 0)
            {
                fs.Write(buffer, 0, len);
            }
            fs.Close();
        }
    }
}

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号