public static bool unzipFile(Stream inputStream, string targetDirectory, bool overwrite, OnUnzipProgress callback) { bool ret = false; callback(0, 0); using (ZipFile zipFile_ = new ZipFile(inputStream)) { int totalBytes = (int)zipFile_.unzipSize; UnzipCach cach = new UnzipCach(); cach.start(totalBytes, callback, overwrite); INameTransform extractNameTransform_ = new WindowsNameTransform(targetDirectory); System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator(); while (enumerator.MoveNext()) { try { ZipEntry entry = (ZipEntry)enumerator.Current; if (entry.IsFile) { string fileName = extractNameTransform_.TransformFile(entry.Name); string dirName = Path.GetDirectoryName(Path.GetFullPath(fileName)); if (!Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } Stream source = zipFile_.GetInputStream(entry); cach.addFile(fileName, (int)entry.Size, source); source.Close(); } else { string dirName = extractNameTransform_.TransformDirectory(entry.Name); if (!Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } } } catch (Exception e) { cach.setState(UnzipCach.State.Error); //LogManager.GetInstance().LogException(e.Message, e, LogManager.ModuleFilter.RES); } if (cach.isError()) { break; } } cach.setState(UnzipCach.State.Ok); ret = cach.stop(); callback(1, ret?0:1); return(ret); } }
// TODO: Replace with clean OFS-based zip methods static void ExtractPackage(IFile wrapFile, IDirectory destinationDirectory) { var nt = new WindowsNameTransform(destinationDirectory.Path.FullPath); using (var zipFile = new ZipFile(wrapFile.OpenRead())) { foreach (ZipEntry zipEntry in zipFile) { if (zipEntry.IsFile) { var filePath = nt.TransformFile(zipEntry.Name); using (var targetFile = destinationDirectory.FileSystem.GetFile(filePath).MustExist().OpenWrite()) using (var sourceFile = zipFile.GetInputStream(zipEntry)) sourceFile.CopyTo(targetFile); // TODO: restore last write time here by adding it to OFS } } } }
protected static bool ExtractPackage(IFile wrapFile, IDirectory destinationDirectory) { try { var nt = new WindowsNameTransform(destinationDirectory.Path.FullPath); using (var zipFile = new ZipFile(wrapFile.OpenRead())) { foreach (ZipEntry zipEntry in zipFile) { if (zipEntry.IsFile) { IFile destinationFile; if (System.IO.Path.DirectorySeparatorChar == '\\') destinationFile = destinationDirectory.FileSystem.GetFile(nt.TransformFile(zipEntry.Name)); else destinationFile = destinationDirectory.GetFile(zipEntry.Name); using (var targetFile = destinationFile.MustExist().OpenWrite()) using (var sourceFile = zipFile.GetInputStream(zipEntry)) StreamExtensions.CopyTo(sourceFile, targetFile); // TODO: restore last write time here by adding it to OFS } } } } catch (Exception e) { return false; } return true; }
static void unzipFileThread() { //LogManager.GetInstance().LogMessage("unzip begin outpath="+_targetDirectory, LogManager.ModuleFilter.RES); _callback(0, 0); bool ret = false; using (ZipFile zipFile_ = new ZipFile(_inputStream)) { int totalBytes = (int)zipFile_.unzipSize; UnzipCach cach = new UnzipCach(); cach.start(totalBytes, _callback); INameTransform extractNameTransform_ = new WindowsNameTransform(_tmpDirectory); System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator(); while (enumerator.MoveNext()) { try { ZipEntry entry = (ZipEntry)enumerator.Current; if (entry.IsFile) { string fileName = extractNameTransform_.TransformFile(entry.Name); string dirName = Path.GetDirectoryName(Path.GetFullPath(fileName)); if (!Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } Stream source = zipFile_.GetInputStream(entry); cach.addFile(fileName, (int)entry.Size, source); source.Close(); } else { string dirName = extractNameTransform_.TransformDirectory(entry.Name); if (!Directory.Exists(dirName)) { Directory.CreateDirectory(dirName); } } } catch (Exception e) { cach.setState(UnzipCach.State.Error); //LogManager.GetInstance().LogException(e.Message, e, LogManager.ModuleFilter.RES); } if (cach.isError()) { break; } } cach.setState(UnzipCach.State.Ok); if (cach.stop()) { try { Directory.Move(_tmpDirectory, _targetDirectory); ret = true; } catch (Exception e) { //LogManager.GetInstance().LogException("unzip rename dir error.", e); } } _callback(1, ret?0:1); //LogManager.GetInstance().LogMessage("unzip end", LogManager.ModuleFilter.RES); _inputStream = null; _callback = null; } }
object extractZip( object zipFileName, string rootDirectory,IStringFilter nf, IStringFilter df) { object ret = null; WindowsNameTransform extractNameTransform = new WindowsNameTransform(rootDirectory); Dictionary<string, bool> dirs = new Dictionary<string, bool>(StringComparer.InvariantCultureIgnoreCase); Stream str; if (zipFileName is byte[]) str = new MemoryStream((byte[])zipFileName); else str=new SeekableStream(Context.OpenStream(zipFileName.ToString()),true); using (ZipFile zip = new ZipFile(str)) { if (Password != null) zip.Password = Context.TransformStr(Password, Transform); foreach (ZipEntry entry in zip) { string targetName = null; if (entry.IsFile) { targetName = extractNameTransform.TransformFile(entry.Name); if (!UsePath) targetName = Path.Combine(rootDirectory, Path.GetFileName(targetName)); } else if (entry.IsDirectory) { if (UsePath) targetName = extractNameTransform.TransformDirectory(entry.Name); else targetName = rootDirectory; } if (string.IsNullOrEmpty(targetName)) continue; if (!Hidden) { if (isDos(entry) && (((FileAttributes) entry.ExternalFileAttributes) & (FileAttributes.Hidden)) != 0) continue; } if (string.IsNullOrEmpty(entry.Name)) continue; var n = new ZipFSEntry(entry, ZipTime); if ((entry.IsFile && df.IsMatch(Path.GetDirectoryName(n.FullName)) && nf.IsMatch(n.Name)) || (entry.IsDirectory && df.IsMatch(n.FullName))) { object r = extract(zip, rootDirectory, targetName, entry, dirs); if (r!=null) return r; } } } return ret; }