mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-07-21 00:15:24 +00:00
Until now the caller had to completely consume each entry stream, or call SkipEntry(), before disposing the stream. If not, exception was thrown: "EntryStream has not been fully consumed". Hugely inconvenient; a user-thrown exception inside a "using (EntryStream)" block would be discarded. Now automatically skips the entry on dispose. Added method EntryStream.Cancel(). Call this if entry stream is unfinished, and no further entries are required. Helps with efficiency, as it avoids reading data that is not needed.
87 lines
2.9 KiB
C#
87 lines
2.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using SharpCompress.Common;
|
|
using SharpCompress.Common.Rar;
|
|
using SharpCompress.Compressor.Rar;
|
|
|
|
namespace SharpCompress.Reader.Rar
|
|
{
|
|
/// <summary>
|
|
/// This class faciliates Reading a Rar Archive in a non-seekable forward-only manner
|
|
/// </summary>
|
|
public abstract class RarReader : AbstractReader<RarReaderEntry, RarVolume>
|
|
{
|
|
public string Password { get; set; }
|
|
private RarVolume volume;
|
|
private readonly Unpack pack = new Unpack();
|
|
|
|
internal RarReader(Options options)
|
|
: base(options, ArchiveType.Rar)
|
|
{
|
|
}
|
|
|
|
internal abstract void ValidateArchive(RarVolume archive);
|
|
|
|
public override RarVolume Volume
|
|
{
|
|
get { return volume; }
|
|
}
|
|
|
|
#region Open
|
|
|
|
/// <summary>
|
|
/// Opens a RarReader for Non-seeking usage with a single volume
|
|
/// </summary>
|
|
/// <param name="stream"></param>
|
|
/// <param name="options"></param>
|
|
/// <returns></returns>
|
|
public static RarReader Open(Stream stream, Options options = Options.KeepStreamsOpen)
|
|
{
|
|
return Open(stream, null, options);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Opens a RarReader for Non-seeking usage with multiple volumes
|
|
/// </summary>
|
|
/// <param name="streams"></param>
|
|
/// <param name="options"></param>
|
|
/// <returns></returns>
|
|
public static RarReader Open(IEnumerable<Stream> streams, Options options = Options.KeepStreamsOpen)
|
|
{
|
|
streams.CheckNotNull("streams");
|
|
return new MultiVolumeRarReader(streams, options);
|
|
}
|
|
|
|
#endregion
|
|
|
|
internal override IEnumerable<RarReaderEntry> GetEntries(Stream stream)
|
|
{
|
|
volume = new RarReaderVolume(stream, Password, Options);
|
|
foreach (RarFilePart fp in volume.ReadFileParts())
|
|
{
|
|
ValidateArchive(volume);
|
|
yield return new RarReaderEntry(volume.IsSolidArchive, fp);
|
|
}
|
|
}
|
|
|
|
protected virtual IEnumerable<FilePart> CreateFilePartEnumerableForCurrentEntry()
|
|
{
|
|
return Entry.Parts;
|
|
}
|
|
|
|
protected override EntryStream GetEntryStream()
|
|
{
|
|
return CreateEntryStream(new RarStream(pack, Entry.FileHeader,
|
|
new MultiVolumeReadOnlyStream(
|
|
CreateFilePartEnumerableForCurrentEntry().Cast<RarFilePart>(), this)));
|
|
}
|
|
|
|
public static RarReader Open(Stream stream, string password, Options options = Options.KeepStreamsOpen)
|
|
{
|
|
stream.CheckNotNull("stream");
|
|
return new SingleVolumeRarReader(stream, password, options);
|
|
}
|
|
}
|
|
} |