C# 파일 압축 클래스[온라인 수집]

6257 단어 C#
1. Charp Devlop에 압축된 DLL이 있는데 무료로 오픈된 Sharp ZipLib이라고 합니다.
다운로드한 후 넷-20의 DLL을 압축해제합니다.
2.이곳은 인터넷에서 수집하는 일반적인 종류이다
  
public class ZipHelper
    {
         /// <summary>
        ///     
        /// </summary>
        /// <param name="sourceFilePath"></param>
        /// <param name="destinationZipFilePath"></param>
        public static void CreateZip(string sourceFilePath, string destinationZipFilePath)
        {
            if (sourceFilePath[sourceFilePath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
                sourceFilePath += System.IO.Path.DirectorySeparatorChar;
 
            ZipOutputStream zipStream = new ZipOutputStream(File.Create(destinationZipFilePath));
            zipStream.SetLevel(6);  //      0-9
            CreateZipFiles(sourceFilePath, zipStream, sourceFilePath);
 
            zipStream.Finish();
            zipStream.Close();
        }
 
        /// <summary>
        ///       
        /// </summary>
        /// <param name="sourceFilePath">            </param>
        /// <param name="zipStream">     zip    (   D:\WorkSpace\a.zip),         .zip   </param>
        /// <param name="staticFile"></param>
        private static void CreateZipFiles(string sourceFilePath, ZipOutputStream zipStream, string staticFile)
        {
            Crc32 crc = new Crc32();
            string[] filesArray = Directory.GetFileSystemEntries(sourceFilePath);
            foreach (string file in filesArray)
            {
                if (Directory.Exists(file))                     //
                {
                    CreateZipFiles(file, zipStream, staticFile);
                }
 
                else                                            //
                {
                    FileStream fileStream = File.OpenRead(file);
 
                    byte[] buffer = new byte[fileStream.Length];
                    fileStream.Read(buffer, 0, buffer.Length);
                    string tempFile = file.Substring(staticFile.LastIndexOf("\\") + 1);
                    ZipEntry entry = new ZipEntry(tempFile);
 
                    entry.DateTime = DateTime.Now;
                    entry.Size = fileStream.Length;
                    fileStream.Close();
                    crc.Reset();
                    crc.Update(buffer);
                    entry.Crc = crc.Value;
                    zipStream.PutNextEntry(entry);
 
                    zipStream.Write(buffer, 0, buffer.Length);
                }
            }
        }
    }

3. 호출 방식
    ZipHelper.CreateZip(pathFile,@"d:\test.zip");

좋은 웹페이지 즐겨찾기