2016-11-17 15 views
0

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ライブラリを使用したいと考えています。

+0

が重複する可能性を/ 17232414/Creating-a-zip-archive-in-memory-using-system-io-compression –

+0

彼の投稿では、zipファイルを作成していますが、zip内のファイルを抽出したいと思います。 MemoryStreamの可能性はこれと何か関係があるかもしれませんが、私はそれをどうやってやるかを知りたいです。 –

+0

これを確認してください:https://stackoverflow.com/questions/20520238/how-to-read-file-from-zip-archive-to-memory-with-extracting-it-to-file-first –

答えて

0

_formerRepository.InsertFormerFileAsBinary 

は[]バイトかかることを考えると、これは動作するはずです:

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

そして、参考のために:https://stackoverflow.com/questionsのCreating a byte array from a stream

関連する問題