internal static void Test() { // NOTE: This path is probably wrong, and should not be hard-coded. const string sourceFolderPath = @"C:\Red Stapler Vault\Supporting Files\Standard Library\Standard Library\MailMerging"; var outputFolderPath = TestStatics.OutputFolderPath; IoMethods.DeleteFolder(outputFolderPath); // Create and extract empty zip file var emptyFolderPath = StandardLibraryMethods.CombinePaths(outputFolderPath, "Empty"); Directory.CreateDirectory(emptyFolderPath); var emptyZipPath = StandardLibraryMethods.CombinePaths(outputFolderPath, "empty.zip"); ZipFolderAsFile(emptyFolderPath, emptyZipPath); using (var memoryStream = new MemoryStream()) { ZipFolderAsStream(emptyFolderPath, memoryStream); memoryStream.Reset(); UnZipStreamIntoFolder(memoryStream, StandardLibraryMethods.CombinePaths(outputFolderPath, "Empty from stream")); } // File-based var zipFilePath = StandardLibraryMethods.CombinePaths(outputFolderPath, "file.zip"); var extractedPath = StandardLibraryMethods.CombinePaths(outputFolderPath, "Extracted from File"); ZipFolderAsFile(sourceFolderPath, zipFilePath); UnZipFileAsFolder(zipFilePath, extractedPath); // Byte-array-based var bytes = ZipFolderAsByteArray(sourceFolderPath); var byteZipFilePath = StandardLibraryMethods.CombinePaths(outputFolderPath, "fileFromBytes.zip"); File.WriteAllBytes(byteZipFilePath, bytes); UnZipByteArrayAsFolder(File.ReadAllBytes(byteZipFilePath), StandardLibraryMethods.CombinePaths(outputFolderPath, "Extracted from Byte Array")); // Stream-based var streamedFilePath = StandardLibraryMethods.CombinePaths(outputFolderPath, "fileFromStream.zip"); using (var fs = new FileStream(streamedFilePath, FileMode.Create)) ZipFolderAsStream(sourceFolderPath, fs); var streamedExtractedPath = StandardLibraryMethods.CombinePaths(outputFolderPath, "Extracted from Stream"); using (var memoryStream = new MemoryStream()) { ZipFolderAsStream(sourceFolderPath, memoryStream); memoryStream.Reset(); UnZipStreamIntoFolder(memoryStream, streamedExtractedPath); } using (var fs = File.OpenRead(streamedFilePath)) { var files = UnZipStreamAsFileObjects(fs); ZipFileObjectsAsStream(files, File.OpenWrite(StandardLibraryMethods.CombinePaths(outputFolderPath, "fileFromStreamedFileObjects.zip"))); } }
/// <summary> /// Unzips the specified ZIP file into a new folder with the specified path. If a folder already exists at the path, it is deleted. /// </summary> public static void UnZipFileAsFolder(string sourceFilePath, string destinationFolderPath) { IoMethods.DeleteFolder(destinationFolderPath); new FastZip().ExtractZip(sourceFilePath, destinationFolderPath, null); }
/// <summary> /// Unzips the specified ZIP stream into a new folder with the specified path. If a folder already exists at the path, it is deleted. /// </summary> public static void UnZipStreamAsFolder(Stream sourceStream, string destinationFolderPath) { IoMethods.DeleteFolder(destinationFolderPath); UnZipStreamIntoFolder(sourceStream, destinationFolderPath); }
internal static void Test() { const string outputFolderName = "PdfOpsTests"; var outputFolder = StandardLibraryMethods.CombinePaths(TestStatics.OutputFolderPath, outputFolderName); IoMethods.DeleteFolder(outputFolder); Directory.CreateDirectory(outputFolder); var inputTestFiles = StandardLibraryMethods.CombinePaths(TestStatics.InputTestFilesFolderPath, "PdfOps"); var onePagePdfPath = StandardLibraryMethods.CombinePaths(inputTestFiles, "onepage.pdf"); var twoPagePdfPath = StandardLibraryMethods.CombinePaths(inputTestFiles, "twopage.pdf"); var threePagePdfPath = StandardLibraryMethods.CombinePaths(inputTestFiles, "threepage.pdf"); var explanations = new List <Tuple <String, String> >(); //ConcatPdfs using (var onePage = File.OpenRead(onePagePdfPath)) { const string concatOnePdf = "ConcatOne.pdf"; using (var concatFile = File.OpenWrite(StandardLibraryMethods.CombinePaths(outputFolder, concatOnePdf))) ConcatPdfs(onePage.ToSingleElementArray(), concatFile); explanations.Add(Tuple.Create(concatOnePdf, "This file should be exactly the same as {0}.".FormatWith(onePagePdfPath))); resetFileStream(onePage); using (var twoPage = File.OpenRead(twoPagePdfPath)) { const string concatTwoPdfs = "ConcatTwo.pdf"; using (var concatFile = File.OpenWrite(StandardLibraryMethods.CombinePaths(outputFolder, concatTwoPdfs))) ConcatPdfs(new[] { onePage, twoPage }, concatFile); explanations.Add(Tuple.Create(concatTwoPdfs, "This file should look like {0} immediately followed by {1}.".FormatWith(onePagePdfPath, twoPagePdfPath))); resetFileStream(onePage, twoPage); using (var threePage = File.OpenRead(threePagePdfPath)) { const string concatThreePdfs = "ConcatThree.pdf"; using (var concatFile = File.OpenWrite(StandardLibraryMethods.CombinePaths(outputFolder, concatThreePdfs))) ConcatPdfs(new[] { onePage, twoPage, threePage }, concatFile); explanations.Add(Tuple.Create(concatThreePdfs, "This file should look like {0} immediately followed by {1} immediately followed by {2}.".FormatWith(onePagePdfPath, twoPagePdfPath, threePagePdfPath))); } } } //CreateBookmarkedPdf using (var onePage = new MemoryStream()) { IoMethods.CopyStream(File.OpenRead(onePagePdfPath), onePage); const string bookmarkOnePdf = "BookmarkOne.pdf"; const string bookmarkTitle = "Bookmark 1"; using (var bookmarkFile = File.OpenWrite(StandardLibraryMethods.CombinePaths(outputFolder, bookmarkOnePdf))) CreateBookmarkedPdf(Tuple.Create(bookmarkTitle, onePage).ToSingleElementArray(), bookmarkFile); explanations.Add(Tuple.Create(bookmarkOnePdf, "This should be {0} labeled with one bookmark named {1}.".FormatWith(onePagePdfPath, bookmarkTitle))); using (var twoPage = new MemoryStream()) { IoMethods.CopyStream(File.OpenRead(twoPagePdfPath), twoPage); const string bookmarkTwoPdf = "BookmarkTwo.pdf"; const string firstBookmarkTitle = "First bookmark"; const string secondBookmarkTitle = "Second bookmark"; using (var bookmarkFile = File.OpenWrite(StandardLibraryMethods.CombinePaths(outputFolder, bookmarkTwoPdf))) CreateBookmarkedPdf(new[] { Tuple.Create(firstBookmarkTitle, onePage), Tuple.Create(secondBookmarkTitle, twoPage) }, bookmarkFile); explanations.Add(Tuple.Create(bookmarkTwoPdf, "This should be {0} labeled with bookmark named {1} followed by {2} with the title of {3}.".FormatWith(onePagePdfPath, firstBookmarkTitle, twoPagePdfPath, secondBookmarkTitle))); using (var threePage = new MemoryStream()) { IoMethods.CopyStream(File.OpenRead(threePagePdfPath), threePage); const string bookmarkThreePdf = "BookmarkThree.pdf"; const string thirdBookmarkTItle = "Third bookmark"; using (var bookmarkFile = File.OpenWrite(StandardLibraryMethods.CombinePaths(outputFolder, bookmarkThreePdf))) { CreateBookmarkedPdf( new[] { Tuple.Create(firstBookmarkTitle, onePage), Tuple.Create(secondBookmarkTitle, twoPage), Tuple.Create(thirdBookmarkTItle, threePage) }, bookmarkFile); } explanations.Add(Tuple.Create(bookmarkThreePdf, "This should be {0} labeled with bookmark named {1} followed by {2} with the title of {3} followed by {4} with the title of {5}." .FormatWith(onePagePdfPath, firstBookmarkTitle, twoPagePdfPath, secondBookmarkTitle, threePagePdfPath, thirdBookmarkTItle))); } } } TestStatics.OutputReadme(outputFolder, explanations); }