public IEnumerable <string> Extract(ZipExtractOption option) { if (!File.Exists(option.ZipFile)) { throw new FileNotFoundException(option.ZipFile); } var outputFolder = GetOutputFolder(option); Directory.CreateDirectory(outputFolder); using (var s = new ZipInputStream(File.OpenRead(option.ZipFile))) { if (option.Password.IsNotNullOrEmpty()) { s.Password = option.Password; } ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { if (theEntry.IsDirectory) { var dir = Path.Combine(outputFolder, theEntry.Name); Directory.CreateDirectory(dir); yield return(dir); continue; } var fileName = Path.Combine(outputFolder, theEntry.Name); Directory.CreateDirectory(Path.GetDirectoryName(fileName)); if (File.Exists(fileName)) { if (!option.IsOverwriteIfExisted) { throw new InvalidOperationException($"File {fileName} is already exisited."); } else { File.Delete(fileName); } } using (var streamWriter = File.Create(fileName)) { var data = new byte[2048]; while (true) { var size = s.Read(data, 0, data.Length); if (size <= 0) { break; } streamWriter.Write(data, 0, size); } } yield return(fileName); } } }
private string GetOutputFolder(ZipExtractOption option) => Path.GetFullPath(option.OutputFolder ?? Path.GetDirectoryName(option.ZipFile));