我有一个应用程序需要读取包含完整目录和多个文件的 Zip 文件。
我可以读取每个 pdf 并将其写入磁盘,然后进行合并。然而,I/O 是愚蠢的。我想阅读 zip 文件中的每个 pdf 并合并为一合一的方法。
我只能找到这个使用 iTextSharp 的示例,该示例已被弃用。
这是我到目前为止所拥有的:
public static void ProcessZipFileTest(string zipFile)
{
List<string> pdfFiles = new List<string>();
try
{
MemoryStream finalStream = new MemoryStream();
using (ZipArchive archive = ZipFile.OpenRead(zipFile))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.FullName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
{
//// Write the file to disk for combining separately
//string destinationPath = Path.GetFullPath(Path.Combine(ProcessingFolder, Path.GetFileName(entry.FullName)));
//entry.ExtractToFile(destinationPath);
//pdfFiles.Add(destinationPath);
PdfCopyFields copy = new PdfCopyFields(finalStream); // this is the iTextSharp way and outdated
string file1Path = "Sample1.pdf";
string file2Path = "Sample2.pdf";
var ms1 = new MemoryStream(File.ReadAllBytes(file1Path));
ms1.Position = 0;
copy.AddDocument(new PdfReader(ms1));
ms1.Dispose();
}
}
}
// write the final stream to disk
}
catch (Exception e)
{
// TODO LOG FAILURE
}
根据反馈,我将写入磁盘并合并。将测试速度。