|
| 1 | +#region Apache License |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +#endregion |
| 16 | + |
| 17 | +//********************************************************************************** |
| 18 | +//* クラス名 :UnZipperV2 |
| 19 | +//* クラス日本語名 :SharpZipLibを使用した解凍クラス |
| 20 | +//* |
| 21 | +//* 作成者 :玄人 幸道 |
| 22 | +//* 更新履歴 : |
| 23 | +//* |
| 24 | +//* 日時 更新者 内容 |
| 25 | +//* ---------- ---------------- ------------------------------------------------- |
| 26 | +//* 2026/08/08 玄人 幸道 新規作成(#524) |
| 27 | +//********************************************************************************** |
| 28 | + |
| 29 | +using System; |
| 30 | +using System.Collections.Generic; |
| 31 | +using System.IO; |
| 32 | +using System.Text; |
| 33 | + |
| 34 | +using ICSharpCode.SharpZipLib.Zip; |
| 35 | + |
| 36 | +namespace Touryo.Infrastructure.Public.IO |
| 37 | +{ |
| 38 | + /// <summary>SharpZipLibを使用した解凍クラス</summary> |
| 39 | + /// <remarks> |
| 40 | + /// 旧 UnZipper(DotNetZip)の代替(#524)。 |
| 41 | + /// **引数の並びと名前は旧に合わせてある。** 型だけが自前の列挙型に変わる。 |
| 42 | + /// |
| 43 | + /// **StatusMSG は無い。** 旧の呼び出し元(DeployZipPackWithHTTP)は |
| 44 | + /// StatusMSG のログ文言("extract file ...")を解析して解凍先の一覧を得ていたが、 |
| 45 | + /// DotNetZip 固有の文字列であり再現できない。 |
| 46 | + /// **ExtractedFiles で受け取ること。** |
| 47 | + /// </remarks> |
| 48 | + public class UnZipperV2 : ZipBaseV2 |
| 49 | + { |
| 50 | + #region ExtractedFiles |
| 51 | + |
| 52 | + /// <summary>解凍したファイルのパス</summary> |
| 53 | + private List<string> _extractedFiles = new List<string>(); |
| 54 | + |
| 55 | + /// <summary>解凍したファイルのパス</summary> |
| 56 | + /// <remarks> |
| 57 | + /// 旧 StatusMSG の解析で得ていたものの代替。 |
| 58 | + /// **ExtractFileFromZip のたびに作り直す**(前回の内容は残らない)。 |
| 59 | + /// 上書きせず飛ばしたファイルは含まない。 |
| 60 | + /// </remarks> |
| 61 | + public string[] ExtractedFiles |
| 62 | + { |
| 63 | + get { return this._extractedFiles.ToArray(); } |
| 64 | + } |
| 65 | + |
| 66 | + #endregion |
| 67 | + |
| 68 | + #region ExtractFileFromZip |
| 69 | + |
| 70 | + /// <summary>ZIPファイルを解凍</summary> |
| 71 | + /// <param name="zipFileName">ZIPファイル名</param> |
| 72 | + /// <param name="directoryToUnZip">解凍先</param> |
| 73 | + /// <param name="selectionDlgt">ファイル選択デリゲード</param> |
| 74 | + /// <param name="selectionCriteriaInfo">ファイル選択基準情報</param> |
| 75 | + /// <param name="extractExistingFile">上書き時の動作</param> |
| 76 | + /// <param name="enc">エンコーディング</param> |
| 77 | + /// <param name="zipPassword">パスワード</param> |
| 78 | + public void ExtractFileFromZip( |
| 79 | + string zipFileName, |
| 80 | + string directoryToUnZip, |
| 81 | + SelectionDelegate selectionDlgt, |
| 82 | + object selectionCriteriaInfo, |
| 83 | + ExtractExistingFileActionV2 extractExistingFile, |
| 84 | + Encoding enc, |
| 85 | + string zipPassword) |
| 86 | + { |
| 87 | + // ファイル選択基準 |
| 88 | + base.SetSelectionCriteria(selectionDlgt, selectionCriteriaInfo); |
| 89 | + |
| 90 | + this._extractedFiles = new List<string>(); |
| 91 | + |
| 92 | + using (FileStream fs = File.OpenRead(zipFileName)) |
| 93 | + using (ZipFile zip = new ZipFile(fs, false, base.GetStringCodec(enc))) |
| 94 | + { |
| 95 | + if (!string.IsNullOrEmpty(zipPassword)) { zip.Password = zipPassword; } |
| 96 | + |
| 97 | + ZipProgressEventArgsV2 e = new ZipProgressEventArgsV2( |
| 98 | + ZipProgressEventTypeV2.Extracting_Started, zipFileName); |
| 99 | + e.EntriesTotal = (int)zip.Count; |
| 100 | + base.OnExtractProgress(e); |
| 101 | + |
| 102 | + int processed = 0; |
| 103 | + |
| 104 | + foreach (ZipEntry entry in zip) |
| 105 | + { |
| 106 | + // ディレクトリのエントリは、ファイルの展開時に必要に応じて作る。 |
| 107 | + if (!entry.IsFile) { continue; } |
| 108 | + |
| 109 | + // 解凍対象ファイルを選択(ファイル選択デリゲードを使用) |
| 110 | + if (base.SelectionDlgt(entry.Name, base.SelectionCriteriaInfo)) |
| 111 | + { |
| 112 | + this.ExtractEntry(zip, entry, zipFileName, directoryToUnZip, |
| 113 | + extractExistingFile, (int)zip.Count, processed); |
| 114 | + } |
| 115 | + |
| 116 | + processed++; |
| 117 | + } |
| 118 | + |
| 119 | + e = new ZipProgressEventArgsV2( |
| 120 | + ZipProgressEventTypeV2.Extracting_Completed, zipFileName); |
| 121 | + e.EntriesTotal = (int)zip.Count; |
| 122 | + e.EntriesProcessed = processed; |
| 123 | + base.OnExtractProgress(e); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + #endregion |
| 128 | + |
| 129 | + #region private |
| 130 | + |
| 131 | + /// <summary>1エントリを解凍する</summary> |
| 132 | + /// <param name="zip">ZipFile</param> |
| 133 | + /// <param name="entry">ZipEntry</param> |
| 134 | + /// <param name="zipFileName">書庫のファイル名</param> |
| 135 | + /// <param name="directoryToUnZip">解凍先</param> |
| 136 | + /// <param name="extractExistingFile">上書き時の動作</param> |
| 137 | + /// <param name="entriesTotal">エントリの総数</param> |
| 138 | + /// <param name="entriesProcessed">処理済みのエントリ数</param> |
| 139 | + private void ExtractEntry( |
| 140 | + ZipFile zip, |
| 141 | + ZipEntry entry, |
| 142 | + string zipFileName, |
| 143 | + string directoryToUnZip, |
| 144 | + ExtractExistingFileActionV2 extractExistingFile, |
| 145 | + int entriesTotal, |
| 146 | + int entriesProcessed) |
| 147 | + { |
| 148 | + string path = UnZipperV2.GetSafePath(directoryToUnZip, entry.Name); |
| 149 | + |
| 150 | + #region 既存ファイルの扱い |
| 151 | + |
| 152 | + if (File.Exists(path)) |
| 153 | + { |
| 154 | + if (extractExistingFile == ExtractExistingFileActionV2.Throw) |
| 155 | + { |
| 156 | + throw new IOException(path); |
| 157 | + } |
| 158 | + else if (extractExistingFile == ExtractExistingFileActionV2.DoNotOverwrite) |
| 159 | + { |
| 160 | + ZipProgressEventArgsV2 skip = new ZipProgressEventArgsV2( |
| 161 | + ZipProgressEventTypeV2.Extracting_ExtractEntryWouldOverwrite, zipFileName); |
| 162 | + skip.EntriesTotal = entriesTotal; |
| 163 | + skip.EntriesProcessed = entriesProcessed; |
| 164 | + skip.CurrentEntryName = entry.Name; |
| 165 | + base.OnExtractProgress(skip); |
| 166 | + |
| 167 | + return; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + #endregion |
| 172 | + |
| 173 | + string dir = Path.GetDirectoryName(path); |
| 174 | + if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) |
| 175 | + { |
| 176 | + Directory.CreateDirectory(dir); |
| 177 | + } |
| 178 | + |
| 179 | + ZipProgressEventArgsV2 e = new ZipProgressEventArgsV2( |
| 180 | + ZipProgressEventTypeV2.Extracting_BeforeExtractEntry, zipFileName); |
| 181 | + e.EntriesTotal = entriesTotal; |
| 182 | + e.EntriesProcessed = entriesProcessed; |
| 183 | + e.CurrentEntryName = entry.Name; |
| 184 | + e.TotalBytesToTransfer = entry.Size; |
| 185 | + base.OnExtractProgress(e); |
| 186 | + |
| 187 | + byte[] buffer = new byte[81920]; |
| 188 | + long transferred = 0; |
| 189 | + |
| 190 | + using (Stream src = zip.GetInputStream(entry)) |
| 191 | + using (FileStream dst = File.Create(path)) |
| 192 | + { |
| 193 | + int read = src.Read(buffer, 0, buffer.Length); |
| 194 | + |
| 195 | + while (0 < read) |
| 196 | + { |
| 197 | + dst.Write(buffer, 0, read); |
| 198 | + transferred += read; |
| 199 | + |
| 200 | + e = new ZipProgressEventArgsV2( |
| 201 | + ZipProgressEventTypeV2.Extracting_EntryBytesWritten, zipFileName); |
| 202 | + e.EntriesTotal = entriesTotal; |
| 203 | + e.EntriesProcessed = entriesProcessed; |
| 204 | + e.CurrentEntryName = entry.Name; |
| 205 | + e.TotalBytesToTransfer = entry.Size; |
| 206 | + e.BytesTransferred = transferred; |
| 207 | + base.OnExtractProgress(e); |
| 208 | + |
| 209 | + read = src.Read(buffer, 0, buffer.Length); |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + // 更新日時を書庫の値に合わせる。 |
| 214 | + File.SetLastWriteTime(path, entry.DateTime); |
| 215 | + |
| 216 | + this._extractedFiles.Add(path); |
| 217 | + |
| 218 | + e = new ZipProgressEventArgsV2( |
| 219 | + ZipProgressEventTypeV2.Extracting_AfterExtractEntry, zipFileName); |
| 220 | + e.EntriesTotal = entriesTotal; |
| 221 | + e.EntriesProcessed = entriesProcessed + 1; |
| 222 | + e.CurrentEntryName = entry.Name; |
| 223 | + e.TotalBytesToTransfer = entry.Size; |
| 224 | + e.BytesTransferred = transferred; |
| 225 | + base.OnExtractProgress(e); |
| 226 | + } |
| 227 | + |
| 228 | + /// <summary>解凍先のパスを求める</summary> |
| 229 | + /// <param name="directoryToUnZip">解凍先</param> |
| 230 | + /// <param name="entryName">エントリ名</param> |
| 231 | + /// <returns>解凍先のパス</returns> |
| 232 | + /// <remarks> |
| 233 | + /// **解凍先の外に出るエントリ名を弾く(Zip Slip 対策)。** |
| 234 | + /// エントリ名は書庫を作った側が自由に決められるため、 |
| 235 | + /// "../" を含めて任意の場所へ書かせることができる。 |
| 236 | + /// 旧実装が使っていた DotNetZip は、この脆弱性 |
| 237 | + /// (GHSA-xhg6-9j5j-w4vf)で非推奨になった。**同じ轍を踏まない。** |
| 238 | + /// </remarks> |
| 239 | + private static string GetSafePath(string directoryToUnZip, string entryName) |
| 240 | + { |
| 241 | + string root = Path.GetFullPath(directoryToUnZip); |
| 242 | + string path = Path.GetFullPath(Path.Combine(root, entryName)); |
| 243 | + |
| 244 | + // 区切り文字を足してから比較する。 |
| 245 | + // 足さないと "C:\dir" と "C:\dir2" が前方一致してしまう。 |
| 246 | + string prefix = root.EndsWith(Path.DirectorySeparatorChar.ToString()) |
| 247 | + ? root : root + Path.DirectorySeparatorChar; |
| 248 | + |
| 249 | + if (!path.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) |
| 250 | + { |
| 251 | + throw new IOException(entryName); |
| 252 | + } |
| 253 | + |
| 254 | + return path; |
| 255 | + } |
| 256 | + |
| 257 | + #endregion |
| 258 | + } |
| 259 | +} |
0 commit comments