C# 压缩整个文件夹

///    
        /// 压缩文件夹   
        ///    
        /// 要打包的文件夹   
        /// 目标文件名   
        /// 压缩品质级别(0~9)   
        /// 是否删除原文件夹 
        public static void CompressDirectory(string dirPath, string GzipFileName, bool deleteDir)
        {
            //压缩文件路径
            string pCompressPath = dirPath + “.zip”;
            //创建压缩文件
            FileStream pCompressFile = new FileStream(pCompressPath, FileMode.Create);
            using (ZipOutputStream zipoutputstream = new ZipOutputStream(pCompressFile))
            {
                Crc32 crc = new Crc32();
                Dictionary fileList = GetAllFies(dirPath);
                foreach (KeyValuePair item in fileList)
                {
                    FileStream fs = new FileStream(item.Key.ToString(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                    // FileStream fs = File.OpenRead(item.Key.ToString());
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    ZipEntry entry = new ZipEntry(item.Key.Substring(dirPath.Length));
                    entry.DateTime = item.Value;
                    entry.Size = fs.Length;
                    fs.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    zipoutputstream.PutNextEntry(entry);
                    zipoutputstream.Write(buffer, 0, buffer.Length);
                }
            }
            if (deleteDir)
            {
                Directory.Delete(dirPath, true);
            }
        }
        ///    
        /// 获取所有文件   
        ///    
        ///    
        private static Dictionary GetAllFies(string dir)
        {
            Dictionary FilesList = new Dictionary();
            DirectoryInfo fileDire = new DirectoryInfo(dir);
            if (!fileDire.Exists)
            {
                throw new System.IO.FileNotFoundException(“目录:” + fileDire.FullName + “没有找到!”);
            }
            GetAllDirFiles(fileDire, FilesList);
            GetAllDirsFiles(fileDire.GetDirectories(), FilesList);
            return FilesList;
        }
        ///    
        /// 获取一个文件夹下的所有文件夹里的文件   
        ///    
        ///    
        ///    
        private static void GetAllDirsFiles(DirectoryInfo[] dirs, Dictionary filesList)
        {
            foreach (DirectoryInfo dir in dirs)
            {
                foreach (FileInfo file in dir.GetFiles("."))
                {
                    filesList.Add(file.FullName, file.LastWriteTime);
                }
                GetAllDirsFiles(dir.GetDirectories(), filesList);
            }
        }
        ///    
        /// 获取一个文件夹下的文件   
        ///    
        /// 目录名称   
        /// 文件列表HastTable   
        private static void GetAllDirFiles(DirectoryInfo dir, Dictionary filesList)
        {
            foreach (FileInfo file in dir.GetFiles())
            {
                filesList.Add(file.FullName, file.LastWriteTime);
            }
        }

你可能感兴趣的:(AE开发)