【问题标题】:How to store a pdf within a zip file, without saving it locally如何将pdf存储在zip文件中,而不在本地保存
【发布时间】:2016-11-17 09:42:38
【问题描述】:

我在 zip 文件中有一个 zip 文件,在内部 zip 文件中我有一个 Excel 和一个 PDF/Tif 文档。

我目前正在开发一种工具,可以读取所有这些文件并将它们存储在数据库中。在我先将这两个文件存储在一个文件夹中之前,但由于 zip 文件包含大量文件,我收到了磁盘空间不足的异常。

所以现在我想尝试在不知道文件夹位置的情况下直接将文件存储在数据库中。

// Path.Combine(exportPathNoZip,entry.FullName) --> \unzip\Files1\Files1.ZIP
using (ZipArchive archive = ZipFile.OpenRead(Path.Combine(exportPathNoZip,entry.FullName)))
{
    // These are the files inside the zip file and contain the Excel and the image
    foreach (ZipArchiveEntry item in archive.Entries)
    {
        // Save image to database
        if (item.FullName.EndsWith(".tif", StringComparison.OrdinalIgnoreCase) ||
            item.FullName.EndsWith(".tiff", StringComparison.OrdinalIgnoreCase) ||
            item.FullName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
        {
            byte[] file = File.ReadAllBytes(?????);
            _formerRepository.InsertFormerFileAsBinary(file);
        }
    }
}

但这不起作用,因为File.ReadAllBytes() 需要路径。那么我能做什么呢?在本地存储文件会给我一个磁盘空间不足的异常,没有它我没有路径。

请注意,我只想为此使用 System.IO.Compression 库。

【问题讨论】:

标签: c# system.io.compression


【解决方案1】:

鉴于此

_formerRepository.InsertFormerFileAsBinary

需要字节[],那么这应该可以工作:

 using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) {
        item.Open().CopyTo(ms);
        _formerRepository.InsertFormerFileAsBinary(ms.ToArray());
 }

供参考:Creating a byte array from a stream

【讨论】:

    猜你喜欢
    • 2021-11-07
    • 2023-02-22
    • 2019-06-01
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多