FileInfo - OpenReadZipFile
The path to the archive to open, specified as a relative or absolute path. A relative path is interpreted as relative to the current working directory.
public static void Main() { CreateZipFile(); var dir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ExtractZipFileToDirectory")); if (dir.Exists) { dir.Clear(); } var zip = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ZipFileDirectory", "test.zip")); Directory.CreateDirectory(zip.Directory.FullName); string extractPath = dir.FullName; if (!extractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)) extractPath += Path.DirectorySeparatorChar; //C# Extension Method: FileInfo - OpenReadZipFile using (var archive = zip.OpenReadZipFile()) { foreach (ZipArchiveEntry entry in archive.Entries) { Console.WriteLine(entry.FullName); } } } public static void CreateZipFile() { var dir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TxtFileDirectory")); var dir2 = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ZipFileDirectory")); if (dir.Exists) { dir.Clear(); } if (dir2.Exists) { dir2.Clear(); } // Type var @this = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TxtFileDirectory", "test.txt")); var zip = new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ZipFileDirectory", "test.zip")); Directory.CreateDirectory(@this.Directory.FullName); Directory.CreateDirectory(zip.Directory.FullName); // Intialization using (FileStream stream = @this.Create()) { } @this.Directory.CreateZipFile(zip); }
View Source
#if NET45_OR_GREATER using System.IO; using System.IO.Compression; public static partial class Extensions { /// <summary> /// The path to the archive to open, specified as a relative or absolute path. A relative path is interpreted as /// relative to the current working directory. /// </summary> /// <param name="this"> /// The path to the archive to open, specified as a relative or absolute path. A relative path is /// interpreted as relative to the current working directory. /// </param> /// <returns>The opened zip archive.</returns> public static ZipArchive OpenReadZipFile(this FileInfo @this) { return ZipFile.OpenRead(@this.FullName); } } #endif