public void Rar_Multi_Reader_Delete_Files() { var testArchives = new string[] { "Rar.multi.part01.rar", "Rar.multi.part02.rar", "Rar.multi.part03.rar", "Rar.multi.part04.rar", "Rar.multi.part05.rar", "Rar.multi.part06.rar" }; ResetScratch(); foreach (var file in testArchives) { File.Copy(Path.Combine(TEST_ARCHIVES_PATH, file), Path.Combine(SCRATCH2_FILES_PATH, file)); } using (var reader = RarReader.Open(testArchives.Select(s => Path.Combine(SCRATCH2_FILES_PATH, s)) .Select(p => File.OpenRead(p)), Options.None)) { while (reader.MoveToNextEntry()) { reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); } } VerifyFiles(); foreach (var file in testArchives.Select(s => Path.Combine(SCRATCH2_FILES_PATH, s))) { File.Delete(file); } }
private void DoRar_Multi_Reader_Delete_Files(string[] archives) { foreach (var file in archives) { File.Copy(Path.Combine(TEST_ARCHIVES_PATH, file), Path.Combine(SCRATCH2_FILES_PATH, file)); } var streams = archives.Select(s => Path.Combine(SCRATCH2_FILES_PATH, s)).Select(File.OpenRead).ToList(); using (var reader = RarReader.Open(streams)) { while (reader.MoveToNextEntry()) { reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); } } foreach (var stream in streams) { stream.Dispose(); } VerifyFiles(); foreach (var file in archives.Select(s => Path.Combine(SCRATCH2_FILES_PATH, s))) { File.Delete(file); } }
static void ExtractRars(string file) { var filename = Path.GetFileNameWithoutExtension(file); var rars = Directory.GetFiles(Path.GetDirectoryName(file), filename + ".r??").ToList() .Where(f => !f.EndsWith(".rar")).OrderBy(x => x.ToString()).ToList(); rars.Insert(0, file); var streams = rars.Select(File.OpenRead).ToList(); Console.WriteLine($"Extracting rar file {file}"); using (var reader = RarReader.Open(streams)) { while (reader.MoveToNextEntry()) { reader.WriteEntryToDirectory(Path.GetDirectoryName(file), new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); Console.WriteLine($"extracted rar file {reader.Entry.Key}"); } } foreach (var stream in streams) { stream.Dispose(); } foreach (var rar in rars) { File.Delete(rar); } }
protected override IReader CreateReaderForSolidExtraction() { Stream stream = Enumerable.First <RarVolume>(base.Volumes).Stream; stream.Position = 0L; return(RarReader.Open(stream, Options.KeepStreamsOpen)); }
public void Rar_Entry_Stream() { ResetScratch(); using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.rar"))) using (var reader = RarReader.Open(stream)) { while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) { Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar); using (var entryStream = reader.OpenEntryStream()) { string file = Path.GetFileName(reader.Entry.Key); string folder = Path.GetDirectoryName(reader.Entry.Key); string destdir = Path.Combine(SCRATCH_FILES_PATH, folder); if (!Directory.Exists(destdir)) { Directory.CreateDirectory(destdir); } string destinationFileName = Path.Combine(destdir, file); using (FileStream fs = File.OpenWrite(destinationFileName)) { entryStream.TransferTo(fs); } } } } } VerifyFiles(); }
//Get List of File in RAR public List <string> GetFilesInRar(string source) { var list = new List <string>(); try { using (Stream stream = File.OpenRead(source)) { var reader = RarReader.Open(stream); while (reader.MoveToNextEntry()) { if (reader.ArchiveType == ArchiveType.Rar) { list.Add(reader.Entry.FilePath); } } } return(list); } catch (Exception ex) { throw new ArgumentException(ex.Message); } }
//[Fact] public void Rar_Multi_Reader_Encrypted() { var testArchives = new string[] { "EncryptedParts.part01.rar", "EncryptedParts.part02.rar", "EncryptedParts.part03.rar", "EncryptedParts.part04.rar", "EncryptedParts.part05.rar", "EncryptedParts.part06.rar" }; ResetScratch(); using (var reader = RarReader.Open(testArchives.Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) .Select(p => File.OpenRead(p)))) { while (reader.MoveToNextEntry()) { reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); } } VerifyFiles(); }
protected override IReader CreateReaderForSolidExtraction() { var stream = Volumes.First().Stream; stream.Position = 0; return(RarReader.Open(stream, ReaderOptions)); }
private void DoRar_Multi_Reader_Encrypted(string[] archives) { Assert.Throws <InvalidFormatException>(() => { ResetScratch(); using (var reader = RarReader.Open(archives.Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) .Select(p => File.OpenRead(p)), new ReaderOptions() { Password = "******" })) { while (reader.MoveToNextEntry()) { reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); } } VerifyFiles(); }); }
public override bool TestUnZip(string filePath) { try { using (var stream = File.OpenRead(filePath)) { IReader reader = null; if (IsEncryptFile(filePath)) { if (ZipArchive.IsZipFile(filePath)) { reader = ZipReader.Open(stream, new ReaderOptions { Password = "******" }); } else if (RarArchive.IsRarFile(filePath)) { reader = RarReader.Open(stream, new ReaderOptions { Password = "******" }); } } else { reader = ReaderFactory.Open(stream); } var subGuidDir = new DirectoryInfo(Path.Combine(ZipBase.UnZipRootDir, Guid.NewGuid().ToString("N"))); subGuidDir.Create(); if (reader != null) { while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) { reader.WriteEntryToDirectory(subGuidDir.FullName, new ExtractionOptions { ExtractFullPath = true, Overwrite = true }); } } return(VerifyManager.Verify(subGuidDir.FullName)); } } } catch (Exception ex) { Debug.WriteLine(ex); } return(false); }
/// <summary> /// Opens a Reader for Non-seeking usage /// </summary> /// <param name="stream"></param> /// <param name="options"></param> /// <returns></returns> public static IReader Open(Stream stream, ReaderOptions options = null) { stream.CheckNotNull("stream"); options = options ?? new ReaderOptions() { LeaveStreamOpen = false }; RewindableStream rewindableStream = new RewindableStream(stream); rewindableStream.StartRecording(); if (ZipArchive.IsZipFile(rewindableStream, options.Password)) { rewindableStream.Rewind(true); return(ZipReader.Open(rewindableStream, options)); } rewindableStream.Rewind(false); if (GZipArchive.IsGZipFile(rewindableStream)) { rewindableStream.Rewind(false); GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress); if (TarArchive.IsTarFile(testStream)) { rewindableStream.Rewind(true); return(new TarReader(rewindableStream, options, CompressionType.GZip)); } rewindableStream.Rewind(true); return(GZipReader.Open(rewindableStream, options)); } rewindableStream.Rewind(false); if (BZip2Stream.IsBZip2(rewindableStream)) { rewindableStream.Rewind(false); BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, true); if (TarArchive.IsTarFile(testStream)) { rewindableStream.Rewind(true); return(new TarReader(rewindableStream, options, CompressionType.BZip2)); } } rewindableStream.Rewind(false); if (RarArchive.IsRarFile(rewindableStream, options)) { rewindableStream.Rewind(true); return(RarReader.Open(rewindableStream, options)); } rewindableStream.Rewind(false); if (TarArchive.IsTarFile(rewindableStream)) { rewindableStream.Rewind(true); return(TarReader.Open(rewindableStream, options)); } throw new InvalidOperationException("Cannot determine compressed stream type. Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar"); }
/// <summary> /// Opens a Reader for Non-seeking usage /// </summary> /// <param name="stream"></param> /// <param name="options"></param> /// <returns></returns> public static IReader Open(Stream stream, Options options = Options.KeepStreamsOpen) { stream.CheckNotNull("stream"); RewindableStream rewindableStream = new RewindableStream(stream); rewindableStream.StartRecording(); if (ZipArchive.IsZipFile(rewindableStream, null)) { rewindableStream.Rewind(true); return(ZipReader.Open(rewindableStream, null, options)); } rewindableStream.Rewind(false); if (GZipArchive.IsGZipFile(rewindableStream)) { rewindableStream.Rewind(false); GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress); if (TarArchive.IsTarFile(testStream)) { rewindableStream.Rewind(true); return(new TarReader(rewindableStream, CompressionType.GZip, options)); } rewindableStream.Rewind(true); return(GZipReader.Open(rewindableStream, options)); } rewindableStream.Rewind(false); if (BZip2Stream.IsBZip2(rewindableStream)) { rewindableStream.Rewind(false); BZip2Stream testStream = new BZip2Stream(rewindableStream, CompressionMode.Decompress, false); if (TarArchive.IsTarFile(testStream)) { rewindableStream.Rewind(true); return(new TarReader(rewindableStream, CompressionType.BZip2, options)); } } rewindableStream.Rewind(false); if (TarArchive.IsTarFile(rewindableStream)) { rewindableStream.Rewind(true); return(TarReader.Open(rewindableStream, options)); } rewindableStream.Rewind(false); if (RarArchive.IsRarFile(rewindableStream, options)) { rewindableStream.Rewind(true); return(RarReader.Open(rewindableStream, options)); } throw new InvalidOperationException("Cannot determine compressed stream type."); }
public static void TestRar2() { var reader = RarReader.Open(TestRar2Streams()); while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) { Console.WriteLine(reader.Entry.FilePath); reader.WriteEntryToDirectory(@"C:\temp", ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); } } }
public void Rar_Jpg_Reader() { ResetScratch(); using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "RarJpeg.jpg"))) using (var reader = RarReader.Open(stream, Options.LookForHeader)) { while (reader.MoveToNextEntry()) { Assert.AreEqual(reader.Entry.CompressionType, CompressionType.Rar); reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); } } VerifyFiles(); }
public static IReader Open(Stream stream, Options options) { Utility.CheckNotNull(stream, "stream"); RewindableStream stream2 = new RewindableStream(stream); stream2.StartRecording(); if (ZipArchive.IsZipFile(stream2, null)) { stream2.Rewind(true); return(ZipReader.Open(stream2, null, options)); } stream2.Rewind(false); if (GZipArchive.IsGZipFile(stream2)) { stream2.Rewind(false); GZipStream stream3 = new GZipStream(stream2, CompressionMode.Decompress); if (TarArchive.IsTarFile(stream3)) { stream2.Rewind(true); return(new TarReader(stream2, CompressionType.GZip, options)); } stream2.Rewind(true); return(GZipReader.Open(stream2, options)); } stream2.Rewind(false); if (BZip2Stream.IsBZip2(stream2)) { stream2.Rewind(false); BZip2Stream stream4 = new BZip2Stream(stream2, CompressionMode.Decompress, false, false); if (TarArchive.IsTarFile(stream4)) { stream2.Rewind(true); return(new TarReader(stream2, CompressionType.BZip2, options)); } } stream2.Rewind(false); if (TarArchive.IsTarFile(stream2)) { stream2.Rewind(true); return(TarReader.Open(stream2, options)); } stream2.Rewind(false); if (!RarArchive.IsRarFile(stream2, options)) { throw new InvalidOperationException("Cannot determine compressed stream type. Supported Reader Formats: Zip, GZip, BZip2, Tar, Rar"); } stream2.Rewind(true); return(RarReader.Open(stream2, options)); }
public void Rar_Reader_Audio_program() { ResetScratch(); using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Audio_program.rar"))) using (var reader = RarReader.Open(stream, Options.LookForHeader)) { while (reader.MoveToNextEntry()) { Assert.AreEqual(reader.Entry.CompressionType, CompressionType.Rar); reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); } } CompareFilesByPath(Path.Combine(SCRATCH_FILES_PATH, "test.dat"), Path.Combine(MISC_TEST_FILES_PATH, "test.dat")); }
public void Rar_Solid_Skip_Reader() { ResetScratch(); using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.solid.rar"))) using (var reader = RarReader.Open(stream, Options.LookForHeader)) { while (reader.MoveToNextEntry()) { if (reader.Entry.Key.Contains("jpg")) { Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar); reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); } } } }
private void DoRar_Multi_Reader(string[] archives) { using (var reader = RarReader.Open(archives.Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) .Select(p => File.OpenRead(p)))) { while (reader.MoveToNextEntry()) { reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); } } VerifyFiles(); }
private void ReadRar(string testArchive, string password) { ResetScratch(); using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, testArchive))) using (var reader = RarReader.Open(stream, password)) { while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) { Assert.AreEqual(reader.Entry.CompressionType, CompressionType.Rar); reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); } } } VerifyFiles(); }
public void UnpackAll(UnpackOptions options = null) { options = options ?? new UnpackOptions(); if (!ArchiveFiles.Any()) { return; } options.DestinationDirectory = string.IsNullOrWhiteSpace(options.DestinationDirectory) ? Path.GetDirectoryName(ArchiveFiles.First()) : options.DestinationDirectory; var fileStream = ArchiveFiles .Select(File.OpenRead) .ToList(); using (var reader = RarReader.Open(fileStream)) { reader.CompressedBytesRead += CompressedBytesRead; reader.EntryExtractionBegin += EntryExtractionBegin; reader.EntryExtractionEnd += EntryExtractionEnd; reader.FilePartExtractionBegin += FilePartExtractionBegin; reader.WriteAllToDirectory(options.DestinationDirectory, new ExtractionOptions { ExtractFullPath = true, Overwrite = options.Overwrite }); if (options.DeleteArchiveOnSuccess) { foreach (var file in ArchiveFiles) { File.Delete(file); } } foreach (var stream in fileStream) { stream.Close(); stream.Dispose(); } } }
/// <summary> /// Opens a Reader for Non-seeking usage /// </summary> /// <param name="stream"></param> /// <param name="listener"></param> /// <param name="options"></param> /// <returns></returns> public static IReader OpenReader(Stream stream, IExtractionListener listener, Options options = Options.KeepStreamsOpen) { stream.CheckNotNull("stream"); RewindableStream rewindableStream = new RewindableStream(stream); rewindableStream.Recording = true; if (ZipArchive.IsZipFile(rewindableStream)) { return(ZipReader.Open(rewindableStream, listener, options)); } rewindableStream.Rewind(); rewindableStream.Recording = true; if (GZipReader.IsGZip(rewindableStream)) { rewindableStream.Rewind(); GZipStream testStream = new GZipStream(rewindableStream, CompressionMode.Decompress); rewindableStream.Recording = true; if (TarReader.IsTarFile(testStream)) { rewindableStream.Rewind(); return(TarGZipReader.Open(rewindableStream, listener, options)); } rewindableStream.Rewind(); return(GZipReader.Open(rewindableStream, listener, options)); } rewindableStream.Rewind(); rewindableStream.Recording = true; if (TarReader.IsTarFile(rewindableStream)) { rewindableStream.Rewind(); return(TarReader.Open(rewindableStream, listener, options)); } rewindableStream.Rewind(); rewindableStream.Recording = true; if (RarArchive.IsRarFile(rewindableStream)) { rewindableStream.Rewind(); return(RarReader.Open(rewindableStream, listener, options)); } throw new InvalidOperationException("Cannot determine compressed stream type."); }
public async Task FillPipeAsync(Stream sourceStream, PipeWriter writer, CancellationToken cancellationToken) { try { using (var statsStream = new BufferCopyStream(sourceStream)) using (var rarReader = RarReader.Open(statsStream)) while (rarReader.MoveToNextEntry()) { if (!rarReader.Entry.IsDirectory && rarReader.Entry.Key.EndsWith(".log", StringComparison.InvariantCultureIgnoreCase) && !rarReader.Entry.Key.Contains("tty.log", StringComparison.InvariantCultureIgnoreCase)) { LogSize = rarReader.Entry.Size; using (var rarStream = rarReader.OpenEntryStream()) { int read; FlushResult flushed; do { var memory = writer.GetMemory(Config.MinimumBufferSize); read = await rarStream.ReadAsync(memory, cancellationToken); writer.Advance(read); SourcePosition = statsStream.Position; flushed = await writer.FlushAsync(cancellationToken).ConfigureAwait(false); SourcePosition = statsStream.Position; } while (read > 0 && !(flushed.IsCompleted || flushed.IsCanceled || cancellationToken.IsCancellationRequested)); } writer.Complete(); return; } SourcePosition = statsStream.Position; } Config.Log.Warn("No rar entries that match the log criteria"); } catch (Exception e) { Config.Log.Error(e, "Error filling the log pipe"); } writer.Complete(); }
private void DecompressRar() { var archiveOptions = new ReaderOptions(); if (!string.IsNullOrWhiteSpace(ArchivePassword)) { archiveOptions.Password = ArchivePassword; } using (var reader = RarReader.Open(File.OpenRead(ArchivePath), archiveOptions)) { while (reader.MoveToNextEntry()) { reader.WriteEntryToDirectory(OutputDirectory, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); } } //RarArchive archive = RarArchive.Open(ArchivePath); //foreach (RarArchiveEntry entry in archive.Entries) //{ // try // { // string fileName = Path.GetFileName(entry.FilePath); // string rootToFile = Path.GetFullPath(entry.FilePath).Replace(fileName, ""); // if (!Directory.Exists(rootToFile)) // { // Directory.CreateDirectory(rootToFile); // } // entry.WriteToFile(rootToFile + fileName, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); // } // catch (Exception ex) // { // //handle your exception here.. // } //} }
public void Rar_Jpg_Reader() { using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg"))) using (var reader = RarReader.Open(stream, new ReaderOptions() { LookForHeader = true })) { while (reader.MoveToNextEntry()) { Assert.Equal(CompressionType.Rar, reader.Entry.CompressionType); reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); } } VerifyFiles(); }
public void Rar_Reader_Audio_program() { using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.Audio_program.rar"))) using (var reader = RarReader.Open(stream, new ReaderOptions() { LookForHeader = true })) { while (reader.MoveToNextEntry()) { Assert.Equal(CompressionType.Rar, reader.Entry.CompressionType); reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); } } CompareFilesByPath(Path.Combine(SCRATCH_FILES_PATH, "test.dat"), Path.Combine(MISC_TEST_FILES_PATH, "test.dat")); }
public void Rar_Multi_Reader_Delete_Files() { var testArchives = new string[] { "Rar.multi.part01.rar", "Rar.multi.part02.rar", "Rar.multi.part03.rar", "Rar.multi.part04.rar", "Rar.multi.part05.rar", "Rar.multi.part06.rar" }; ResetScratch(); foreach (var file in testArchives) { File.Copy(Path.Combine(TEST_ARCHIVES_PATH, file), Path.Combine(SCRATCH2_FILES_PATH, file)); } var streams = testArchives.Select(s => Path.Combine(SCRATCH2_FILES_PATH, s)).Select(File.OpenRead).ToList(); using (var reader = RarReader.Open(streams)) { while (reader.MoveToNextEntry()) { reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); } } foreach (var stream in streams) { stream.Dispose(); } VerifyFiles(); foreach (var file in testArchives.Select(s => Path.Combine(SCRATCH2_FILES_PATH, s))) { File.Delete(file); } }
//Extract File in RAR public bool RarExtract(string source, string destPath) { try { using (Stream stream = File.OpenRead(source)) { var reader = RarReader.Open(stream); while (reader.MoveToNextEntry()) { if (reader.ArchiveType == ArchiveType.Rar) { reader.WriteEntryToDirectory(destPath, ExtractOptions.ExtractFullPath | ExtractOptions.Overwrite); } } } return(true); } catch (Exception ex) { throw new ArgumentException(ex.Message); } }
private void DoRar_Reader_Skip(string filename) { using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename))) using (var reader = RarReader.Open(stream, new ReaderOptions() { LookForHeader = true })) { while (reader.MoveToNextEntry()) { if (reader.Entry.Key.Contains("jpg")) { Assert.Equal(CompressionType.Rar, reader.Entry.CompressionType); reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); } } } }
public void Rar_Reader_Skip() { ResetScratch(); using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.rar"))) using (var reader = RarReader.Open(stream, new ReaderOptions() { LookForHeader = true })) { while (reader.MoveToNextEntry()) { if (reader.Entry.Key.Contains("jpg")) { Assert.Equal(reader.Entry.CompressionType, CompressionType.Rar); reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); } } } }
/*[Fact] * public void Rar5_Encrypted_Reader() * { * ReadRar("Rar5.encrypted_filesOnly.rar", "test"); * }*/ private void ReadRar(string testArchive, string password) { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, testArchive))) using (var reader = RarReader.Open(stream, new ReaderOptions() { Password = password })) { while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) { Assert.Equal(CompressionType.Rar, reader.Entry.CompressionType); reader.WriteEntryToDirectory(SCRATCH_FILES_PATH, new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }); } } } VerifyFiles(); }