cxi DoctNetZipパッケージを使用した操作zipファイル(作成/読み込み/更新)の例


ダウンロード先はここです。http://dotnetzip.codeplex.com/
ダウンロードしたカバンの中には多くのdllファイルがあります。一般的にはIonic.Zip.dllを参照すればいいです。
次にこの名前空間を参照します。
using Ionic.Zip;
以下は自分でカプセル化したタイプです。

/// <summary>
    /// Zip DotNetZip
    /// </summary>
    public static class ZipUtils
    {
        /// <summary>
        /// ZIP 【 】
        /// </summary>
        /// <param name="sourceStream"></param>
        /// <returns></returns>
        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;
        }


        /// <summary>
        /// ZIP
        /// ,
        /// </summary>
        /// <param name="sourceStream"></param>
        /// <returns></returns>
        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;
        }

        /// <summary>
        /// ZIP
        /// ,
        /// </summary>
        /// <param name="list"> </param>
        /// <param name="strZipName"> </param>
        /// <param name="IsDirStruct"> </param>
        /// <returns> :true/ :false</returns>
        public static bool CompressMulti(List<string> 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;
            }
        }

        /// <summary>
        /// ZIP
        /// </summary>
        /// <param name="strZipPath"> ZIP </param>
        /// <param name="strUnZipPath"> </param>
        /// <param name="overWrite"> </param>
        /// <returns> :true/ :false</returns>
        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<string> list = new List<string>();
  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);