public void PackFiles(string sourceDirectory, IList <string> sourceFileNames, IList <string> fileNames, CompressionLevel compLevel, EventHandler <ArchiveProgressEventArgs> progressHandler) { if (sourceFileNames == null) { throw new ArgumentNullException("sourceFileNames"); } if (fileNames == null) { string[] array = new string[sourceFileNames.Count]; for (int i = 0; i < sourceFileNames.Count; i = checked (i + 1)) { array[i] = Path.GetFileName(sourceFileNames[i]); } fileNames = array; } else if (fileNames.Count != sourceFileNames.Count) { throw new ArgumentOutOfRangeException("fileNames"); } using CompressionEngine compressionEngine = CreateCompressionEngine(); compressionEngine.Progress += progressHandler; IDictionary <string, string> files = CreateStringDictionary(fileNames, sourceFileNames); ArchiveFileStreamContext archiveFileStreamContext = new ArchiveFileStreamContext(FullName, sourceDirectory, files); archiveFileStreamContext.EnableOffsetOpen = true; compressionEngine.CompressionLevel = compLevel; compressionEngine.Pack(archiveFileStreamContext, fileNames); }
/// <summary> /// Compresses files into the archive, specifying the names used to /// store the files in the archive. /// </summary> /// <param name="sourceDirectory">This parameter may be null, but if /// specified it is the root directory /// for any relative paths in <paramref name="fileNames"/>.</param> /// <param name="fileNames">A mapping from internal file paths to /// external file paths.</param> /// <param name="compLevel">The compression level used when creating /// the archive.</param> /// <param name="progressHandler">Handler for receiving progress information; /// this may be null if progress is not desired.</param> public void PackFileSet( string sourceDirectory, IDictionary <string, string> fileNames, CompressionLevel compLevel, EventHandler <ArchiveProgressEventArgs> progressHandler) { if (fileNames == null) { throw new ArgumentNullException("fileNames"); } var fileNamesArray = new string[fileNames.Count]; fileNames.Keys.CopyTo(fileNamesArray, 0); using (var compressionEngine = CreateCompressionEngine()) { compressionEngine.Progress += progressHandler; var streamContext = new ArchiveFileStreamContext( FullName, sourceDirectory, fileNames); streamContext.EnableOffsetOpen = true; compressionEngine.CompressionLevel = compLevel; compressionEngine.Pack(streamContext, fileNamesArray); } }
public void Unpack(string destDirectory, EventHandler <ArchiveProgressEventArgs> progressHandler) { using CompressionEngine compressionEngine = CreateCompressionEngine(); compressionEngine.Progress += progressHandler; ArchiveFileStreamContext archiveFileStreamContext = new ArchiveFileStreamContext(FullName, destDirectory, null); archiveFileStreamContext.EnableOffsetOpen = true; compressionEngine.Unpack(archiveFileStreamContext, null); }
public void UnpackFileSet(IDictionary <string, string> fileNames, string destDirectory, EventHandler <ArchiveProgressEventArgs> progressHandler) { if (fileNames == null) { throw new ArgumentNullException("fileNames"); } using CompressionEngine compressionEngine = CreateCompressionEngine(); compressionEngine.Progress += progressHandler; ArchiveFileStreamContext archiveFileStreamContext = new ArchiveFileStreamContext(FullName, destDirectory, fileNames); archiveFileStreamContext.EnableOffsetOpen = true; compressionEngine.Unpack(archiveFileStreamContext, (string match) => fileNames.ContainsKey(match)); }
private IList <ArchiveFileInfo> InternalGetFiles(Predicate <string> fileFilter) { using CompressionEngine compressionEngine = CreateCompressionEngine(); ArchiveFileStreamContext archiveFileStreamContext = new ArchiveFileStreamContext(FullName, null, null); archiveFileStreamContext.EnableOffsetOpen = true; IList <ArchiveFileInfo> fileInfo = compressionEngine.GetFileInfo(archiveFileStreamContext, fileFilter); for (int i = 0; i < fileInfo.Count; i = checked (i + 1)) { fileInfo[i].Archive = this; } return(fileInfo); }
/// <summary> /// Uses a CompressionEngine to get ArchiveFileInfo objects from this /// archive, and then associates them with this ArchiveInfo instance. /// </summary> /// <param name="fileFilter">Optional predicate that can determine /// which files to process.</param> /// <returns>A list of <see cref="ArchiveFileInfo"/> objects, each /// containing information about a file in the archive.</returns> private IList <ArchiveFileInfo> InternalGetFiles(Predicate <string> fileFilter) { using (CompressionEngine compressionEngine = this.CreateCompressionEngine()) { ArchiveFileStreamContext streamContext = new ArchiveFileStreamContext(this.FullName, null, null); streamContext.EnableOffsetOpen = true; IList <ArchiveFileInfo> files = compressionEngine.GetFileInfo(streamContext, fileFilter); for (int i = 0; i < files.Count; i++) { files[i].Archive = this; } return(files); } }
/// <summary> /// Compresses files into the archive, specifying the names used to /// store the files in the archive. /// </summary> /// <param name="sourceDirectory">This parameter may be null, but if /// specified it is the root directory /// for any relative paths in <paramref name="sourceFileNames"/>.</param> /// <param name="sourceFileNames">The list of files to be included in /// the archive.</param> /// <param name="fileNames">The names of the files as they are stored in /// the archive. Each name includes the internal path of the file, if any. /// This parameter may be null, in which case the files are stored in the /// archive with their source file names and no path information.</param> /// <param name="compLevel">The compression level used when creating the /// archive.</param> /// <param name="progressHandler">Handler for receiving progress information; /// this may be null if progress is not desired.</param> /// <remarks> /// Duplicate items in the <paramref name="fileNames"/> array will cause /// an <see cref="ArchiveException"/>. /// </remarks> public void PackFiles( string sourceDirectory, IList <string> sourceFileNames, IList <string> fileNames, CompressionLevel compLevel, EventHandler <ArchiveProgressEventArgs> progressHandler) { if (sourceFileNames == null) { throw new ArgumentNullException("sourceFileNames"); } if (fileNames == null) { var fileNamesArray = new string[sourceFileNames.Count]; for (var i = 0; i < sourceFileNames.Count; i++) { fileNamesArray[i] = Path.GetFileName(sourceFileNames[i]); } fileNames = fileNamesArray; } else if (fileNames.Count != sourceFileNames.Count) { throw new ArgumentOutOfRangeException("fileNames"); } using (var compressionEngine = CreateCompressionEngine()) { compressionEngine.Progress += progressHandler; var contextFiles = CreateStringDictionary(fileNames, sourceFileNames); var streamContext = new ArchiveFileStreamContext( FullName, sourceDirectory, contextFiles); streamContext.EnableOffsetOpen = true; compressionEngine.CompressionLevel = compLevel; compressionEngine.Pack(streamContext, fileNames); } }
/// <summary> /// Compresses files into the archive, specifying the names used to /// store the files in the archive. /// </summary> /// <param name="sourceDirectory">This parameter may be null, but if /// specified it is the root directory /// for any relative paths in <paramref name="sourceFileNames"/>.</param> /// <param name="sourceFileNames">The list of files to be included in /// the archive.</param> /// <param name="fileNames">The names of the files as they are stored in /// the archive. Each name includes the internal path of the file, if any. /// This parameter may be null, in which case the files are stored in the /// archive with their source file names and no path information.</param> /// <param name="compLevel">The compression level used when creating the /// archive.</param> /// <param name="progressHandler">Handler for receiving progress information; /// this may be null if progress is not desired.</param> /// <remarks> /// Duplicate items in the <paramref name="fileNames"/> array will cause /// an <see cref="ArchiveException"/>. /// </remarks> internal void PackFiles( string sourceDirectory, IList<string> sourceFileNames, IList<string> fileNames, CompressionLevel compLevel, EventHandler<ArchiveProgressEventArgs> progressHandler) { if (sourceFileNames == null) { throw new ArgumentNullException("sourceFileNames"); } if (fileNames == null) { string[] fileNamesArray = new string[sourceFileNames.Count]; for (int i = 0; i < sourceFileNames.Count; i++) { fileNamesArray[i] = Path.GetFileName(sourceFileNames[i]); } fileNames = fileNamesArray; } else if (fileNames.Count != sourceFileNames.Count) { throw new ArgumentOutOfRangeException("fileNames"); } using (CompressionEngine compressionEngine = this.CreateCompressionEngine()) { compressionEngine.Progress += progressHandler; IDictionary<string, string> contextFiles = ArchiveInfo.CreateStringDictionary(fileNames, sourceFileNames); ArchiveFileStreamContext streamContext = new ArchiveFileStreamContext( this.FullName, sourceDirectory, contextFiles); streamContext.EnableOffsetOpen = true; compressionEngine.CompressionLevel = compLevel; compressionEngine.Pack(streamContext, fileNames); } }
public void CabFileStreamContextNullParams() { ArchiveFileStreamContext streamContext = null; Exception caughtEx = null; try { streamContext = new ArchiveFileStreamContext(null); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Passing null to constructor."); caughtEx = null; try { streamContext = new ArchiveFileStreamContext(new string[] { }, "testDir", new Dictionary<string, string>()); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Passing 0-length array to constructor."); caughtEx = null; try { streamContext = new ArchiveFileStreamContext(new string[] { "test.cab" }, null, null); } catch (Exception ex) { caughtEx = ex; } Assert.IsNull(caughtEx); }
public void CabinetUtfPaths() { string[] files = new string[] { "어그리먼트送信ポート1ßà_Agreement.txt", "콘토소ßà_MyProfile.txt", "파트너1ßà_PartnerProfile.txt", }; string dirA = "utf8-A"; if (Directory.Exists(dirA)) Directory.Delete(dirA, true); Directory.CreateDirectory(dirA); string dirB = "utf8-B"; if (Directory.Exists(dirB)) Directory.Delete(dirB, true); Directory.CreateDirectory(dirB); int txtSize = 1024; CompressionTestUtil.GenerateRandomFile(Path.Combine(dirA, files[0]), 0, txtSize); CompressionTestUtil.GenerateRandomFile(Path.Combine(dirA, files[1]), 1, txtSize); CompressionTestUtil.GenerateRandomFile(Path.Combine(dirA, files[2]), 2, txtSize); ArchiveFileStreamContext streamContextA = new ArchiveFileStreamContext("utf8.cab", dirA, null); using (CabEngine cabEngine = new CabEngine()) { cabEngine.Pack(streamContextA, files); } ArchiveFileStreamContext streamContextB = new ArchiveFileStreamContext("utf8.cab", dirB, null); using (CabEngine cex = new CabEngine()) { cex.Unpack(streamContextB, null); } bool directoryMatch = CompressionTestUtil.CompareDirectories(dirA, dirB); Assert.IsTrue(directoryMatch, "Testing whether cabinet output directory matches input directory."); }
public void CabinetOffset() { int txtSize = 10240; CompressionTestUtil.GenerateRandomFile("test.txt", 0, txtSize); CompressionTestUtil.GenerateRandomFile("base.txt", 1, 2 * txtSize + 4); ArchiveFileStreamContext streamContext = new ArchiveFileStreamContext("base.txt", null, null); streamContext.EnableOffsetOpen = true; using (CabEngine cabEngine = new CabEngine()) { cabEngine.Pack(streamContext, new string[] { "test.txt" }); } Assert.IsTrue(new FileInfo("base.txt").Length > 2 * txtSize + 4); string saveText; using (Stream txtStream = File.OpenRead("test.txt")) { saveText = new StreamReader(txtStream).ReadToEnd(); } File.Delete("test.txt"); using (CabEngine cex = new CabEngine()) { cex.Unpack(streamContext, null); } string testText; using (Stream txtStream = File.OpenRead("test.txt")) { testText = new StreamReader(txtStream).ReadToEnd(); } Assert.AreEqual<string>(saveText, testText); }
public void CabinetExtractUpdate() { int fileCount = 5, fileSize = 2048; string dirA = String.Format("{0}-{1}-A", fileCount, fileSize); if (Directory.Exists(dirA)) Directory.Delete(dirA, true); Directory.CreateDirectory(dirA); string dirB = String.Format("{0}-{1}-B", fileCount, fileSize); if (Directory.Exists(dirB)) Directory.Delete(dirB, true); Directory.CreateDirectory(dirB); string[] files = new string[fileCount]; for (int iFile = 0; iFile < fileCount; iFile++) { files[iFile] = "€" + iFile + ".txt"; CompressionTestUtil.GenerateRandomFile(Path.Combine(dirA, files[iFile]), iFile, fileSize); } CabInfo cabInfo = new CabInfo("testupdate.cab"); cabInfo.Pack(dirA); cabInfo.Unpack(dirB); DateTime originalTime = File.GetLastWriteTime(Path.Combine(dirA, "€1.txt")); DateTime pastTime = originalTime - new TimeSpan(0, 5, 0); DateTime futureTime = originalTime + new TimeSpan(0, 5, 0); using (CabEngine cabEngine = new CabEngine()) { string cabName = "testupdate.cab"; ArchiveFileStreamContext streamContext = new ArchiveFileStreamContext(cabName, dirB, null); streamContext.ExtractOnlyNewerFiles = true; Assert.AreEqual<bool>(true, streamContext.ExtractOnlyNewerFiles); Assert.IsNotNull(streamContext.ArchiveFiles); Assert.AreEqual<int>(1, streamContext.ArchiveFiles.Count); Assert.AreEqual<string>(cabName, streamContext.ArchiveFiles[0]); Assert.AreEqual<string>(dirB, streamContext.Directory); File.SetLastWriteTime(Path.Combine(dirB, "€1.txt"), futureTime); cabEngine.Unpack(streamContext, null); Assert.IsTrue(File.GetLastWriteTime(Path.Combine(dirB, "€1.txt")) - originalTime > new TimeSpan(0, 4, 55)); File.SetLastWriteTime(Path.Combine(dirB, "€1.txt"), pastTime); File.SetLastWriteTime(Path.Combine(dirB, "€2.txt"), pastTime); File.SetAttributes(Path.Combine(dirB, "€2.txt"), FileAttributes.ReadOnly); File.SetAttributes(Path.Combine(dirB, "€2.txt"), FileAttributes.Hidden); File.SetAttributes(Path.Combine(dirB, "€2.txt"), FileAttributes.System); cabEngine.Unpack(streamContext, null); Assert.IsTrue((File.GetLastWriteTime(Path.Combine(dirB, "€1.txt")) - originalTime).Duration() < new TimeSpan(0, 0, 5)); // Just test the rest of the streamContext properties here. IDictionary<string, string> testMap = new Dictionary<string, string>(); streamContext = new ArchiveFileStreamContext(cabName, dirB, testMap); Assert.AreSame(testMap, streamContext.Files); Assert.IsFalse(streamContext.EnableOffsetOpen); streamContext.EnableOffsetOpen = true; Assert.IsTrue(streamContext.EnableOffsetOpen); streamContext = new ArchiveFileStreamContext(cabName, ".", testMap); Assert.AreEqual<string>(".", streamContext.Directory); string[] testArchiveFiles = new string[] { cabName }; streamContext = new ArchiveFileStreamContext(testArchiveFiles, ".", testMap); Assert.AreSame(testArchiveFiles, streamContext.ArchiveFiles); } }
internal void UnpackFileSet( IDictionary<string, string> fileNames, string destDirectory, EventHandler<ArchiveProgressEventArgs> progressHandler) { if (fileNames == null) { throw new ArgumentNullException("fileNames"); } using (CompressionEngine compressionEngine = this.CreateCompressionEngine()) { compressionEngine.Progress += progressHandler; ArchiveFileStreamContext streamContext = new ArchiveFileStreamContext(this.FullName, destDirectory, fileNames); streamContext.EnableOffsetOpen = true; compressionEngine.Unpack( streamContext, delegate(string match) { return fileNames.ContainsKey(match); }); } }
public static void TestCompressionEngineNullParams( CompressionEngine engine, ArchiveFileStreamContext streamContext, string[] testFiles) { Exception caughtEx; Console.WriteLine("Testing null streamContext."); caughtEx = null; try { engine.Pack(null, testFiles); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); caughtEx = null; try { engine.Pack(null, testFiles, 0); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); Console.WriteLine("Testing null files."); caughtEx = null; try { engine.Pack(streamContext, null); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); Console.WriteLine("Testing null files."); caughtEx = null; try { engine.Pack(streamContext, null, 0); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); Console.WriteLine("Testing null stream."); caughtEx = null; try { engine.IsArchive(null); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); caughtEx = null; try { engine.FindArchiveOffset(null); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); caughtEx = null; try { engine.GetFiles(null); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); caughtEx = null; try { engine.GetFileInfo(null); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); caughtEx = null; try { engine.Unpack(null, "testUnpack.txt"); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); Console.WriteLine("Testing null streamContext."); caughtEx = null; try { engine.GetFiles(null, null); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); caughtEx = null; try { engine.GetFileInfo(null, null); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); caughtEx = null; try { engine.Unpack((IUnpackStreamContext) null, null); } catch (Exception ex) { caughtEx = ex; } Assert.IsInstanceOfType(caughtEx, typeof(ArgumentNullException), "Caught exception: " + caughtEx); }
/// <summary> /// Compresses files into the archive, specifying the names used to /// store the files in the archive. /// </summary> /// <param name="sourceDirectory">This parameter may be null, but if /// specified it is the root directory /// for any relative paths in <paramref name="fileNames"/>.</param> /// <param name="fileNames">A mapping from internal file paths to /// external file paths.</param> /// <param name="compLevel">The compression level used when creating /// the archive.</param> /// <param name="progressHandler">Handler for receiving progress information; /// this may be null if progress is not desired.</param> internal void PackFileSet( string sourceDirectory, IDictionary<string, string> fileNames, CompressionLevel compLevel, EventHandler<ArchiveProgressEventArgs> progressHandler) { if (fileNames == null) { throw new ArgumentNullException("fileNames"); } string[] fileNamesArray = new string[fileNames.Count]; fileNames.Keys.CopyTo(fileNamesArray, 0); using (CompressionEngine compressionEngine = this.CreateCompressionEngine()) { compressionEngine.Progress += progressHandler; ArchiveFileStreamContext streamContext = new ArchiveFileStreamContext( this.FullName, sourceDirectory, fileNames); streamContext.EnableOffsetOpen = true; compressionEngine.CompressionLevel = compLevel; compressionEngine.Pack(streamContext, fileNamesArray); } }
internal void Unpack( string destDirectory, EventHandler<ArchiveProgressEventArgs> progressHandler) { using (CompressionEngine compressionEngine = this.CreateCompressionEngine()) { compressionEngine.Progress += progressHandler; ArchiveFileStreamContext streamContext = new ArchiveFileStreamContext(this.FullName, destDirectory, null); streamContext.EnableOffsetOpen = true; compressionEngine.Unpack(streamContext, null); } }
public void CabEngineNullParams() { string[] testFiles = new string[] { "test.txt" }; ArchiveFileStreamContext streamContext = new ArchiveFileStreamContext("test.cab", null, null); using (CabEngine cabEngine = new CabEngine()) { cabEngine.CompressionLevel = CompressionLevel.None; CompressionTestUtil.TestCompressionEngineNullParams( cabEngine, streamContext, testFiles); } }
/// <summary> /// Uses a CompressionEngine to get ArchiveFileInfo objects from this /// archive, and then associates them with this ArchiveInfo instance. /// </summary> /// <param name="fileFilter">Optional predicate that can determine /// which files to process.</param> /// <returns>A list of <see cref="ArchiveFileInfo"/> objects, each /// containing information about a file in the archive.</returns> private IList<ArchiveFileInfo> InternalGetFiles(Predicate<string> fileFilter) { using (CompressionEngine compressionEngine = this.CreateCompressionEngine()) { ArchiveFileStreamContext streamContext = new ArchiveFileStreamContext(this.FullName, null, null); streamContext.EnableOffsetOpen = true; IList<ArchiveFileInfo> files = compressionEngine.GetFileInfo(streamContext, fileFilter); for (int i = 0; i < files.Count; i++) { files[i].Archive = this; } return files; } }
public void CabEngineNoTempFileTest() { int txtSize = 10240; CompressionTestUtil.GenerateRandomFile("testnotemp.txt", 0, txtSize); ArchiveFileStreamContext streamContext = new ArchiveFileStreamContext("testnotemp.cab", null, null); using (CabEngine cabEngine = new CabEngine()) { cabEngine.UseTempFiles = false; cabEngine.Pack(streamContext, new string[] { "testnotemp.txt" }); } new CabInfo("testnotemp.cab").UnpackFile("testnotemp.txt", "testnotemp2.txt"); Assert.AreEqual(txtSize, new FileInfo("testnotemp2.txt").Length); }