c \ # DotNetZip 패 키 징 클래스 를 사용 하여 zip 파일 을 조작 합 니 다 (생 성 / 읽 기 / 업데이트) 인 스 턴 스

다운로드 주소 가 여기에 있 습 니 다:http://dotnetzip.codeplex.com/
다운로드 한 가방 에는 dll 파일 이 많 습 니 다. 보통 Ionic. Zip. dll 을 참조 하면 됩 니 다.
그리고 이 네 임 스페이스 를 참조 하 십시오:
using Ionic.Zip;
다음은 내 가 봉 한 종류 이다.
 
  
///
    /// Zip DotNetZip
    ///

    public static class ZipUtils
    {
        ///
        /// ZIP 【 】
        ///

        ///
        ///
        public static Stream ZipCompress(Stream sourceStream, string entryName = "zip")
        {
            MemoryStream compressedStream = new MemoryStream();
            if (sourceStream != null)
            {
                long sourceOldPosition = 0;
                try
                {
                    sourceOldPosition = sourceStream.Position;
                    sourceStream.Position = 0;
                    using (ZipFile zip = new ZipFile())
                    {
                        zip.AddEntry(entryName, sourceStream);
                        zip.Save(compressedStream);
                        compressedStream.Position = 0;
                    }
                }
                catch
                {
                }
                finally
                {
                    try
                    {
                        sourceStream.Position = sourceOldPosition;
                    }
                    catch
                    {
                    }
                }
            }
            return compressedStream;
        }


        ///


        /// ZIP
        /// ,
        ///

        ///
        ///
        public static Stream ZipDecompress(byte[] data)
        {
            Stream decompressedStream = new MemoryStream();
            if (data != null)
            {
                try
                {
                    MemoryStream dataStream = new MemoryStream(data);
                    using (ZipFile zip = ZipFile.Read(dataStream))
                    {
                        if (zip.Entries.Count > 0)
                        {
                            zip.Entries.First().Extract(decompressedStream);
                            // Extract ms, Stream ,
                            // Stream
                            decompressedStream.Position = 0;
                        }
                    }
                }
                catch
                {
                }
            }
            return decompressedStream;
        }

        ///


        /// ZIP
        /// ,
        ///

        ///
        ///
        ///
        /// :true/ :false
        public static bool CompressMulti(List list, string strZipName, bool IsDirStruct)
        {
            try
            {
                using (ZipFile zip = new ZipFile(Encoding.Default))// ,
                {
                    foreach (string path in list)
                    {
                        string fileName = Path.GetFileName(path);//
                        //
                        if (Directory.Exists(path))
                        {
                            if (IsDirStruct)//
                            {
                                zip.AddDirectory(path, fileName);
                            }
                            else// Zip
                            {
                                zip.AddDirectory(path);
                            }
                        }
                        if (File.Exists(path))//
                        {
                            zip.AddFile(path);
                        }
                    }
                    zip.Save(strZipName);//
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }

        ///


        /// ZIP
        ///

        /// ZIP
        ///
        ///
        /// :true/ :false
        public static bool Decompression(string strZipPath, string strUnZipPath, bool overWrite)
        {
            try
            {
                ReadOptions options = new ReadOptions();
                options.Encoding = Encoding.Default;// ,
                using (ZipFile zip = ZipFile.Read(strZipPath, options))
                {
                    foreach (ZipEntry entry in zip)
                    {
                        if (string.IsNullOrEmpty(strUnZipPath))
                        {
                            strUnZipPath = strZipPath.Split('.').First();
                        }
                        if (overWrite)
                        {
                            entry.Extract(strUnZipPath, ExtractExistingFileAction.OverwriteSilently);// ,
                        }
                        else
                        {
                            entry.Extract(strUnZipPath, ExtractExistingFileAction.DoNotOverwrite);// ,
                        }
                    }
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }


    }


사용 방법:
1. 압축 파일
 
  
List list = new List();
  list.Add(@"D:\Test\ss");
  list.Add(@"D:\Test\test1.jpg");
  list.Add(@"D:\ .txt");
  list.Add(@"D:\Test\ss.xml");
  bool isSuc =ZipUtils. CompressMulti(list, "D:\\Test2.zip",true);

2. 압축 해제 파일
 
  
bool isSuc = ZipUtils.Decompression("D:\\Test\\Test1.zip", "D:\\Teest", true);

좋은 웹페이지 즐겨찾기