public IsMatch ( string name ) : bool | ||
name | string | The value to test. |
return | bool |
public virtual bool IsMatch(string name) { bool result = false; if (name != null) { string name2 = ((name.get_Length() > 0) ? Path.GetFullPath(name) : ""); result = nameFilter_.IsMatch(name2); } return(result); }
/// <summary> /// Test a name to see if it matches the filter. /// </summary> /// <param name="name">The name to test.</param> /// <returns>True if the name matches, false otherwise.</returns> /// <remarks><see cref="Path.GetFullPath(string)"/> is used to get the full path before matching.</remarks> public virtual bool IsMatch(string name) { bool result = false; if (name != null) { string cooked = (name.Length > 0) ? Path.GetFullPath(name) : ""; result = nameFilter_.IsMatch(cooked); } return(result); }
/// <summary> /// Extract the contents of a zip file. /// </summary> /// <param name="zipFileName">The zip file to extract from.</param> /// <param name="targetDirectory">The directory to save extracted information in.</param> /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param> /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param> /// <param name="fileFilter">A filter to apply to files.</param> /// <param name="directoryFilter">A filter to apply to directories.</param> /// <param name="restoreDateTime">Flag indicating wether to restore the date and time for extracted files.</param> public void ExtractZip(string zipFileName, string targetDirectory, Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate, string fileFilter, string directoryFilter, bool restoreDateTime) { if ( (overwrite == Overwrite.Prompt) && (confirmDelegate == null) ) { throw new ArgumentNullException("confirmDelegate"); } continueRunning_ = true; overwrite_ = overwrite; confirmDelegate_ = confirmDelegate; targetDirectory_ = targetDirectory; fileFilter_ = new NameFilter(fileFilter); directoryFilter_ = new NameFilter(directoryFilter); restoreDateTimeOnExtract_ = restoreDateTime; using ( zipFile_ = new ZipFile(zipFileName) ) { #if !NETCF_1_0 if (password_ != null) { zipFile_.Password = password_; } #endif System.Collections.IEnumerator enumerator = zipFile_.GetEnumerator(); while ( continueRunning_ && enumerator.MoveNext()) { ZipEntry entry = (ZipEntry) enumerator.Current; if ( entry.IsFile ) { if ( directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name) ) { ExtractEntry(entry); } } else if ( entry.IsDirectory ) { if ( directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories ) { ExtractEntry(entry); } } else { // Do nothing for volume labels etc... } } } }
/// <summary> /// Extract the contents of a zip file held in a stream. /// </summary> /// <param name="inputStream">The seekable input stream containing the zip to extract from.</param> /// <param name="targetDirectory">The directory to save extracted information in.</param> /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param> /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param> /// <param name="fileFilter">A filter to apply to files.</param> /// <param name="directoryFilter">A filter to apply to directories.</param> /// <param name="restoreDateTime">Flag indicating whether to restore the date and time for extracted files.</param> /// <param name="isStreamOwner">Flag indicating whether the inputStream will be closed by this method.</param> public void ExtractZip(Stream inputStream, string targetDirectory, Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate, string fileFilter, string directoryFilter, bool restoreDateTime, bool isStreamOwner) { if ((overwrite == Overwrite.Prompt) && (confirmDelegate == null)) { throw new ArgumentNullException("confirmDelegate"); } continueRunning_ = true; overwrite_ = overwrite; confirmDelegate_ = confirmDelegate; extractNameTransform_ = new WindowsNameTransform(targetDirectory); fileFilter_ = new NameFilter(fileFilter); directoryFilter_ = new NameFilter(directoryFilter); restoreDateTimeOnExtract_ = restoreDateTime; using (zipFile_ = new ZipFile(inputStream)) { #if !NETCF_1_0 if (password_ != null) { zipFile_.Password = password_; } #endif zipFile_.IsStreamOwner = isStreamOwner; IEnumerator enumerator = zipFile_.GetEnumerator(); while (continueRunning_ && enumerator.MoveNext()) { ZipEntry entry = (ZipEntry)enumerator.Current; if (entry.IsFile) { // TODO Path.GetDirectory can fail here on invalid characters. if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name)) { ExtractEntry(entry); } } else if (entry.IsDirectory) { if (directoryFilter_.IsMatch(entry.Name) && CreateEmptyDirectories) { ExtractEntry(entry); } } else { // Do nothing for volume labels etc... } } } }
/// <summary> /// Extract the contents of a zip file. /// </summary> /// <param name="zipFileName">The zip file to extract from.</param> /// <param name="targetDirectory">The directory to save extracted information in.</param> /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param> /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param> /// <param name="fileFilter">A filter to apply to files.</param> /// <param name="directoryFilter">A filter to apply to directories.</param> /// <param name="restoreDateTime">Flag indicating wether to restore the date and time for extracted files.</param> public void ExtractZip(string zipFileName, string targetDirectory, string fileFilter, string directoryFilter) { continueRunning_ = true; extractNameTransform_ = new WindowsNameTransform(targetDirectory); fileFilter_ = new NameFilter(fileFilter); directoryFilter_ = new NameFilter(directoryFilter); using (zipFile_ = new ZipFile(zipFileName)) { IEnumerator enumerator = zipFile_.GetEnumerator(); while (continueRunning_ && enumerator.MoveNext()) { var entry = (ZipEntry) enumerator.Current; if (entry.IsFile) { // TODO Path.GetDirectory can fail here on invalid characters. if (directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name)) { ExtractEntry(entry); } } } } }
// nupkg file %-encodes zip entry names. This method decodes entry names before writing to disk. // We must do this, or PathTooLongException may be thrown for some unicode entry names. public static void ExtractZipDecoded(string zipFilePath, string outFolder, string directoryFilter = null) { var zf = new ZipFile(zipFilePath); foreach (ZipEntry zipEntry in zf) { if (!zipEntry.IsFile) continue; var entryFileName = Uri.UnescapeDataString(zipEntry.Name); var buffer = new byte[4096]; var zipStream = zf.GetInputStream(zipEntry); var fullZipToPath = Path.Combine(outFolder, entryFileName); var directoryName = Path.GetDirectoryName(fullZipToPath); var directoryFilter_ = new NameFilter(directoryFilter); if (directoryFilter_.IsMatch(directoryName)) { if (directoryName.Length > 0) { Directory.CreateDirectory(directoryName); } using (FileStream streamWriter = File.Create(fullZipToPath)) { StreamUtils.Copy(zipStream, streamWriter, buffer); } } } zf.Close(); }
/// <summary> /// Extract the contents of a zip file. /// </summary> /// <param name="zipFileName">The zip file to extract from.</param> /// <param name="targetDirectory">The directory to save extracted information in.</param> /// <param name="overwrite">The style of <see cref="Overwrite">overwriting</see> to apply.</param> /// <param name="confirmDelegate">A delegate to invoke when confirming overwriting.</param> /// <param name="fileFilter">A filter to apply to files.</param> /// <param name="directoryFilter">A filter to apply to directories.</param> /// <param name="restoreDateTime">Flag indicating wether to restore the date and time for extracted files.</param> public void ExtractZip(string zipFileName, string targetDirectory, Overwrite overwrite, ConfirmOverwriteDelegate confirmDelegate, string fileFilter, string directoryFilter, bool restoreDateTime) { if ( (overwrite == Overwrite.Prompt) && (confirmDelegate == null) ) { throw new ArgumentNullException("confirmDelegate"); } continueRunning_ = true; overwrite_ = overwrite; confirmDelegate_ = confirmDelegate; targetDirectory_ = targetDirectory; fileFilter_ = new NameFilter(fileFilter); directoryFilter_ = new NameFilter(directoryFilter); restoreDateTimeOnExtract_ = restoreDateTime; using ( inputStream_ = new ZipInputStream(File.OpenRead(zipFileName)) ) { if (password_ != null) { inputStream_.Password = password_; } ZipEntry entry; while ( continueRunning_ && (entry = inputStream_.GetNextEntry()) != null ) { if ( directoryFilter_.IsMatch(Path.GetDirectoryName(entry.Name)) && fileFilter_.IsMatch(entry.Name) ) { ExtractEntry(entry); } } } }
public static async Task ExtractZipForInstall(string zipFilePath, string outFolder) { var zf = new ZipFile(zipFilePath); var directoryFilter = new NameFilter("lib"); var entries = zf.OfType<ZipEntry>().ToArray(); var re = new Regex(@"lib[\\\/][^\\\/]*[\\\/]", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase); try { await Utility.ForEachAsync(entries, (zipEntry) => { if (!zipEntry.IsFile) return; var entryFileName = Uri.UnescapeDataString(zipEntry.Name); var fullZipToPath = Path.Combine(outFolder, entryFileName); var directoryName = Path.GetDirectoryName(fullZipToPath); if (!directoryFilter.IsMatch(directoryName)) return; fullZipToPath = re.Replace(fullZipToPath, "", 1); directoryName = re.Replace(directoryName, "", 1); var buffer = new byte[64*1024]; if (directoryName.Length > 0) { Utility.Retry(() => Directory.CreateDirectory(directoryName), 2); } Utility.Retry(() => { using (var zipStream = zf.GetInputStream(zipEntry)) using (FileStream streamWriter = File.Create(fullZipToPath)) { StreamUtils.Copy(zipStream, streamWriter, buffer); } }, 5); }, 4); } finally { zf.Close(); } }