zipファイル内にzipファイルがあり、内側のzipファイル内にExcelとPDF/Tifドキュメントがあります。ローカルに保存せずにzipファイル内にPDFを保存する方法
私は現在、これらのファイルをすべて読み込んでデータベースに格納するツールを開発しています。以前は両方のファイルをフォルダに保存する前に、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
ライブラリを使用したいと考えています。
が重複する可能性を/ 17232414/Creating-a-zip-archive-in-memory-using-system-io-compression –
彼の投稿では、zipファイルを作成していますが、zip内のファイルを抽出したいと思います。 MemoryStreamの可能性はこれと何か関係があるかもしれませんが、私はそれをどうやってやるかを知りたいです。 –
これを確認してください:https://stackoverflow.com/questions/20520238/how-to-read-file-from-zip-archive-to-memory-with-extracting-it-to-file-first –