/*** * 将当前的entry写到指定目录,保留文件信息,如果文件存在, 覆盖文件 * 返回临时目录 * */ public static string writeEntryToTemp(IArchiveEntry entry) { //string tempDir = ConfigurationManager.AppSettings["tempDir"]; bool successed = false; try { entry.WriteToDirectory(tempDir, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); successed = true; } catch (Exception ex) { if (File.Exists(Path.Combine(tempDir, entry.Key))) { string msg = "!!!!!!!!!!发生异常, 但是文件解压成功:" + ex.Message + ex.StackTrace; MessageUtil.DoAppendTBDetail(msg); LogHelper.WriteExportErrorLog(msg); successed = true; } else { string msg = "!!!!!!!!!!发生异常, 解压失败:" + ex.Message + ex.StackTrace; MessageUtil.DoAppendTBDetail(msg); LogHelper.WriteExportErrorLog(msg); successed = false; } } if (successed) { return(Path.Combine(tempDir, entry.Key)); } else { return(""); } }
/// <summary> /// Extract to specific directory, retaining filename /// </summary> public static void WriteToDirectory(this IArchiveEntry entry, string destinationPath, ExtractOptions options = ExtractOptions.Overwrite) { entry.WriteToDirectory(destinationPath, new NullExtractionListener(), options); }
/// <summary> /// 解压数据操作 /// </summary> /// <param name="e">参数</param> private void DecompressDataWork(DecompressDataStartArgs e) { Exception error = null; bool cancelled = false; try { using (MemoryStream stream = new MemoryStream(e.Data)) { ReaderOptions readerOptions = new ReaderOptions(); readerOptions.ArchiveEncoding.Default = Encoding.Default; using (IArchive archive = ArchiveFactory.Open(stream, readerOptions)) { this.m_Progress.ToComplete = archive.TotalUncompressSize; string deleteEntry = e.DeleteEntry; string lastEntry = e.LastEntry; string destinationDirectory = e.DestinationDirectory; ExtractionOptions extractionOptions = new ExtractionOptions { ExtractFullPath = true, Overwrite = true, PreserveFileTime = true }; IArchiveEntry last = null; foreach (IArchiveEntry entry in archive.Entries) { if (this.m_Cancelled) { cancelled = true; return; } if (entry.IsDirectory) { continue; } if (last == null && entry.Key.Equals(lastEntry, StringComparison.OrdinalIgnoreCase)) { last = entry; continue; } if (entry.Key.Equals(deleteEntry, StringComparison.OrdinalIgnoreCase)) { DeleteFromDirectory(entry, destinationDirectory, extractionOptions); } else { entry.WriteToDirectory(destinationDirectory, extractionOptions); } this.m_Progress.Completed += entry.Size; this.PostDecompressProgressChanged(this.m_Progress, this.m_AsyncOp); } if (last != null) { if (this.m_Cancelled) { cancelled = true; return; } if (last.Key.Equals(deleteEntry, StringComparison.OrdinalIgnoreCase)) { DeleteFromDirectory(last, destinationDirectory, extractionOptions); } else { last.WriteToDirectory(destinationDirectory, extractionOptions); } this.m_Progress.Completed += last.Size; this.PostDecompressProgressChanged(this.m_Progress, this.m_AsyncOp); } } } } catch (Exception exp) { error = exp; } finally { this.DecompressDataAsyncCallback(error, cancelled, this.m_AsyncOp); } }