0

I have a zip file within a zip file and inside the inner zip file I have an Excel and a PDF/Tif document.

I'm currently developing a tool that reads all these files and store them in the database. Before I used to store both files on a folder first, but since the zip files holds loads of files I received an out of disk space exception.

So now I want to try to store the files in the database directly without knowing the folder location.

// 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);
        }
    }
}

But this will not work because the File.ReadAllBytes() will require a path. So what can I do? The storing the files locally will give me a out of disk space exception and without it I have no path.

Please be aware that I'd like to use the System.IO.Compression library for this only.

Jordec
  • 1,516
  • 2
  • 22
  • 43
  • Possible duplicate of https://stackoverflow.com/questions/17232414/creating-a-zip-archive-in-memory-using-system-io-compression – Stefano Balzarotti Nov 17 '16 at 09:45
  • In his post they are creating the zip file, yet I want to extract the files inside the zip. Possible the MemoryStream could have something to do with this, but I'd still like to know how to do it. – Jordec Nov 17 '16 at 09:48
  • Check this: https://stackoverflow.com/questions/20520238/how-to-read-file-from-zip-archive-to-memory-without-extracting-it-to-file-first – Stefano Balzarotti Nov 17 '16 at 09:59

1 Answers1

1

Given that

_formerRepository.InsertFormerFileAsBinary

takes byte[], then this should work:

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

And for reference: Creating a byte array from a stream

Community
  • 1
  • 1
parvee
  • 98
  • 10