Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TryOpen API #435

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/SharpCompress/Archives/ArchiveFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,52 @@ public static IArchive Open(Stream stream, ReaderOptions readerOptions = null)
throw new InvalidOperationException("Cannot determine compressed stream type. Supported Archive Formats: Zip, GZip, Tar, Rar, 7Zip, LZip");
}

public static bool TryOpen(Stream stream, ReaderOptions readerOptions,ArchiveTypeMask archiveTypes, out IArchive archive)
{
stream.CheckNotNull("stream");
if (!stream.CanRead || !stream.CanSeek)
{
throw new ArgumentException("Stream should be readable and seekable");
}
readerOptions = readerOptions ?? new ReaderOptions();
if (archiveTypes.HasFlag(ArchiveTypeMask.Zip) && ZipArchive.IsZipFile(stream, null))
{
stream.Seek(0, SeekOrigin.Begin);
archive = ZipArchive.Open(stream, readerOptions);
return true;
}
stream.Seek(0, SeekOrigin.Begin);
if (archiveTypes.HasFlag(ArchiveTypeMask.SevenZip) && SevenZipArchive.IsSevenZipFile(stream))
{
stream.Seek(0, SeekOrigin.Begin);
archive = SevenZipArchive.Open(stream, readerOptions);
return true;
}
stream.Seek(0, SeekOrigin.Begin);
if (archiveTypes.HasFlag(ArchiveTypeMask.GZip) && GZipArchive.IsGZipFile(stream))
{
stream.Seek(0, SeekOrigin.Begin);
archive = GZipArchive.Open(stream, readerOptions);
return true;
}
stream.Seek(0, SeekOrigin.Begin);
if (archiveTypes.HasFlag(ArchiveTypeMask.Rar) && RarArchive.IsRarFile(stream, readerOptions))
{
stream.Seek(0, SeekOrigin.Begin);
archive = RarArchive.Open(stream, readerOptions);
return true;
}
stream.Seek(0, SeekOrigin.Begin);
if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(stream))
{
stream.Seek(0, SeekOrigin.Begin);
archive = TarArchive.Open(stream, readerOptions);
return true;
}
archive = null;
return false;
}

public static IWritableArchive Create(ArchiveType type)
{
switch (type)
Expand All @@ -84,6 +130,51 @@ public static IWritableArchive Create(ArchiveType type)

#if !NO_FILE

public static bool TryOpen(string filePath, ReaderOptions options, ArchiveTypeMask archiveTypes, out IArchive archive)
{
filePath.CheckNotNullOrEmpty("filePath");
return TryOpen(new FileInfo(filePath), options, archiveTypes, out archive);
}

public static bool TryOpen(FileInfo fileInfo, ReaderOptions options, ArchiveTypeMask archiveTypes, out IArchive archive)
{
fileInfo.CheckNotNull("fileInfo");
options = options ?? new ReaderOptions { LeaveStreamOpen = false };
using (var stream = fileInfo.OpenRead())
{
if (archiveTypes.HasFlag(ArchiveTypeMask.Zip) && ZipArchive.IsZipFile(stream, null))
{
archive = ZipArchive.Open(fileInfo, options);
}
stream.Seek(0, SeekOrigin.Begin);
if (archiveTypes.HasFlag(ArchiveTypeMask.SevenZip) && SevenZipArchive.IsSevenZipFile(stream))
{
archive = SevenZipArchive.Open(fileInfo, options);
}
stream.Seek(0, SeekOrigin.Begin);
if (archiveTypes.HasFlag(ArchiveTypeMask.GZip) && GZipArchive.IsGZipFile(stream))
{
archive = GZipArchive.Open(fileInfo, options);
return true;
}
stream.Seek(0, SeekOrigin.Begin);
if (archiveTypes.HasFlag(ArchiveTypeMask.Rar) && RarArchive.IsRarFile(stream, options))
{
archive = RarArchive.Open(fileInfo, options);
return true;
}
stream.Seek(0, SeekOrigin.Begin);
if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(stream))
{
archive = TarArchive.Open(fileInfo, options);
return true;
}
}
archive = null;
return false;
}


/// <summary>
/// Constructor expects a filepath to an existing file.
/// </summary>
Expand Down
20 changes: 20 additions & 0 deletions src/SharpCompress/Common/ArchiveTypeMask.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;

namespace SharpCompress.Common
{
[Flags]
public enum ArchiveTypeMask
{
None = 0,
Rar = 1,
Zip = 1 << 1,
Tar = 1 << 2,
SevenZip = 1 << 3,
GZip = 1 << 4,
BZip2 = 1 << 5,
LZip = 1 << 6,

AllExceptTar = Rar | Zip | SevenZip | GZip | BZip2 | LZip,
All = AllExceptTar | Tar
}
}
94 changes: 91 additions & 3 deletions src/SharpCompress/Readers/ReaderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public static IReader Open(Stream stream, ReaderOptions options = null)
{
stream.CheckNotNull("stream");
options = options ?? new ReaderOptions()
{
LeaveStreamOpen = false
};
{
LeaveStreamOpen = false
};
RewindableStream rewindableStream = new RewindableStream(stream);
rewindableStream.StartRecording();
if (ZipArchive.IsZipFile(rewindableStream, options.Password))
Expand Down Expand Up @@ -103,5 +103,93 @@ public static IReader Open(Stream stream, ReaderOptions options = null)
}
throw new InvalidOperationException("Cannot determine compressed stream type. Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar, LZip, XZ");
}


public static bool TryOpen(Stream stream, ReaderOptions options,ArchiveTypeMask archiveTypes, out IReader reader)
{
stream.CheckNotNull("stream");
options = options ?? new ReaderOptions()
{
LeaveStreamOpen = false
};
RewindableStream rewindableStream = new RewindableStream(stream);
rewindableStream.StartRecording();
if (archiveTypes.HasFlag(ArchiveTypeMask.Zip)&& ZipArchive.IsZipFile(rewindableStream, options.Password))
{
rewindableStream.Rewind(true);
reader = ZipReader.Open(rewindableStream, options);
return true;
}
rewindableStream.Rewind(false);
if (archiveTypes.HasFlag(ArchiveTypeMask.GZip) && GZipArchive.IsGZipFile(rewindableStream))
{
rewindableStream.Rewind(false);
GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress);
if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
{
rewindableStream.Rewind(true);
reader = new TarReader(rewindableStream, options, CompressionType.GZip);
return true;
}
rewindableStream.Rewind(true);
reader = GZipReader.Open(rewindableStream, options);
return true;
}

rewindableStream.Rewind(false);
if (archiveTypes.HasFlag(ArchiveTypeMask.BZip2) && BZip2Stream.IsBZip2(rewindableStream))
{
rewindableStream.Rewind(false);
BZip2Stream testStream = new BZip2Stream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress, false);
if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
{
rewindableStream.Rewind(true);
reader = new TarReader(rewindableStream, options, CompressionType.BZip2);
return true;
}
}

rewindableStream.Rewind(false);
if (archiveTypes.HasFlag(ArchiveTypeMask.LZip) && LZipStream.IsLZipFile(rewindableStream))
{
rewindableStream.Rewind(false);
LZipStream testStream = new LZipStream(new NonDisposingStream(rewindableStream), CompressionMode.Decompress);
if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
{
rewindableStream.Rewind(true);
reader = new TarReader(rewindableStream, options, CompressionType.LZip);
return true;
}
}
rewindableStream.Rewind(false);
if (archiveTypes.HasFlag(ArchiveTypeMask.Rar) && RarArchive.IsRarFile(rewindableStream, options))
{
rewindableStream.Rewind(true);
reader = RarReader.Open(rewindableStream, options);
return true;
}

rewindableStream.Rewind(false);
if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(rewindableStream))
{
rewindableStream.Rewind(true);
reader = TarReader.Open(rewindableStream, options);
return true;
}
rewindableStream.Rewind(false);
if (XZStream.IsXZStream(rewindableStream))
{
rewindableStream.Rewind(true);
XZStream testStream = new XZStream(rewindableStream);
if (archiveTypes.HasFlag(ArchiveTypeMask.Tar) && TarArchive.IsTarFile(testStream))
{
rewindableStream.Rewind(true);
reader = new TarReader(rewindableStream, options, CompressionType.Xz);
return true;
}
}
reader = null;
return false;
}
}
}