4 using SharpCompress.Archive.GZip;
7 using SharpCompress.Archive.Rar;
10 using SharpCompress.Archive.SevenZip;
13 using SharpCompress.Archive.Tar;
15 using SharpCompress.Archive.Zip;
16 using SharpCompress.Common;
18 namespace SharpCompress.Archive
20 internal class ArchiveFactory
23 /// Opens an Archive for random access
25 /// <param name="stream"></param>
26 /// <param name="options"></param>
27 /// <returns></returns>
28 public static IArchive Open(Stream stream, Options options = Options.KeepStreamsOpen)
30 stream.CheckNotNull("stream");
31 if (!stream.CanRead || !stream.CanSeek)
33 throw new ArgumentException("Stream should be readable and seekable");
36 if (ZipArchive.IsZipFile(stream, null))
38 stream.Seek(0, SeekOrigin.Begin);
39 return ZipArchive.Open(stream, options, null);
42 stream.Seek(0, SeekOrigin.Begin);
43 if (RarArchive.IsRarFile(stream, Options.LookForHeader | Options.KeepStreamsOpen))
45 stream.Seek(0, SeekOrigin.Begin);
46 return RarArchive.Open(stream, options);
50 stream.Seek(0, SeekOrigin.Begin);
51 if (TarArchive.IsTarFile(stream))
53 stream.Seek(0, SeekOrigin.Begin);
54 return TarArchive.Open(stream, options);
58 stream.Seek(0, SeekOrigin.Begin);
59 if (SevenZipArchive.IsSevenZipFile(stream))
61 stream.Seek(0, SeekOrigin.Begin);
62 return SevenZipArchive.Open(stream, options);
66 stream.Seek(0, SeekOrigin.Begin);
67 if (GZipArchive.IsGZipFile(stream))
69 stream.Seek(0, SeekOrigin.Begin);
70 return GZipArchive.Open(stream, options);
73 throw new InvalidOperationException("Cannot determine compressed stream type. Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip");
76 public static IArchive Create(ArchiveType type)
82 return ZipArchive.Create();
87 return TarArchive.Create();
92 throw new NotSupportedException("Cannot create Archives of type: " + type);
97 #if !PORTABLE && !NETFX_CORE
99 /// Constructor expects a filepath to an existing file.
101 /// <param name="filePath"></param>
102 public static IArchive Open(string filePath)
104 return Open(filePath, Options.None);
108 /// Constructor with a FileInfo object to an existing file.
110 /// <param name="fileInfo"></param>
111 public static IArchive Open(FileInfo fileInfo)
113 return Open(fileInfo, Options.None);
117 /// Constructor expects a filepath to an existing file.
119 /// <param name="filePath"></param>
120 /// <param name="options"></param>
121 public static IArchive Open(string filePath, Options options)
123 filePath.CheckNotNullOrEmpty("filePath");
124 return Open(new FileInfo(filePath), options);
128 /// Constructor with a FileInfo object to an existing file.
130 /// <param name="fileInfo"></param>
131 /// <param name="options"></param>
132 public static IArchive Open(FileInfo fileInfo, Options options)
134 fileInfo.CheckNotNull("fileInfo");
135 using (var stream = fileInfo.OpenRead())
137 if (ZipArchive.IsZipFile(stream, null))
140 return ZipArchive.Open(fileInfo, options, null);
143 stream.Seek(0, SeekOrigin.Begin);
144 if (RarArchive.IsRarFile(stream, Options.LookForHeader | Options.KeepStreamsOpen))
147 return RarArchive.Open(fileInfo, options);
151 stream.Seek(0, SeekOrigin.Begin);
152 if (TarArchive.IsTarFile(stream))
155 return TarArchive.Open(fileInfo, options);
159 stream.Seek(0, SeekOrigin.Begin);
160 if (SevenZipArchive.IsSevenZipFile(stream))
163 return SevenZipArchive.Open(fileInfo, options);
167 stream.Seek(0, SeekOrigin.Begin);
168 if (GZipArchive.IsGZipFile(stream))
171 return GZipArchive.Open(fileInfo, options);
174 throw new InvalidOperationException("Cannot determine compressed stream type.");
179 /// Extract to specific directory, retaining filename
181 public static void WriteToDirectory(string sourceArchive, string destinationDirectory,
182 ExtractOptions options = ExtractOptions.Overwrite)
184 using (IArchive archive = Open(sourceArchive))
186 foreach (IArchiveEntry entry in archive.Entries)
188 entry.WriteToDirectory(destinationDirectory, options);