// part of a folder <- ZIP stream /// <summary> /// Unzips the specified ZIP stream to the new or existing folder. If the folder already exists, archive items will be added to the existing folder structure. /// Files will be overwritten when there is an existing file with the same name as an archive file. /// </summary> public static void UnZipStreamIntoFolder(Stream sourceStream, string destinationFolderPath) { Directory.CreateDirectory(destinationFolderPath); using (var zipInputStream = new ZipInputStream(sourceStream)) { ZipEntry entry; while ((entry = zipInputStream.GetNextEntry()) != null) { if (entry.IsDirectory) { Directory.CreateDirectory(StandardLibraryMethods.CombinePaths(destinationFolderPath, entry.Name)); } else { using (var outputStream = IoMethods.GetFileStreamForWrite(StandardLibraryMethods.CombinePaths(destinationFolderPath, entry.Name))) IoMethods.CopyStream(zipInputStream, outputStream); } } } }
/// <summary> /// Concatenates the specified PDF documents and creates bookmarks to the beginning of each file, /// specified by the title passed in the Tuple. /// </summary> /// <param name="outputStream">Stream in which to write</param> /// <param name="bookmarkNamesAndPdfStreams">Title to write in the bookmark, PDF MemoryStream</param> public static void CreateBookmarkedPdf(IEnumerable <Tuple <string, MemoryStream> > bookmarkNamesAndPdfStreams, Stream outputStream) { var concatPdfsStream = new MemoryStream(); using ( concatPdfsStream ) { // Paste all of the PDFs together ConcatPdfs(bookmarkNamesAndPdfStreams.Select(p => p.Item2), concatPdfsStream); } // Add bookmarks to PDF var bookMarkedPdf = addBookmarksToPdf(concatPdfsStream.ToArray(), bookmarkNamesAndPdfStreams.Select(t => Tuple.Create(t.Item1, t.Item2.ToArray()))); // Have the bookmarks displayed on PDF open bookMarkedPdf = setShowBookmarksPaneOnOpen(bookMarkedPdf); // Save the PDf to the output stream using (var bookMarksDisplayedPdf = new MemoryStream(bookMarkedPdf)) IoMethods.CopyStream(bookMarksDisplayedPdf, outputStream); }
/// <summary> /// This method completely ignores directory structure of the zip file in the source stream. The result is a flattened list of files. But, the file names do contain /// the relative path information, so they can be used to re-create the directory structure. /// </summary> public static IEnumerable <RsFile> UnZipStreamAsFileObjects(Stream source) { var files = new List <RsFile>(); using (var zipInputStream = new ZipInputStream(source)) { ZipEntry entry; while ((entry = zipInputStream.GetNextEntry()) != null) { if (entry.IsDirectory) { continue; } using (var outputStream = new MemoryStream()) { IoMethods.CopyStream(zipInputStream, outputStream); files.Add(new RsFile(outputStream.ToArray(), entry.Name)); } } } return(files); }
private static void writeZipEntry(ZipEntry zipEntry, Stream sourceStream, ZipOutputStream zipOutputStream) { zipOutputStream.PutNextEntry(zipEntry); IoMethods.CopyStream(sourceStream, zipOutputStream); }
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); }