0

So what I basically have is a bunch of files stored inside of the database that I want to extract and zip. The one thing i do want to accomplish to doing this all in memory.

I want to send this as an email attachment also.

So basically:

  1. Gather all documents from the database
  2. Gather those documents in an memorystream/array??
  3. Zip those files in memory
  4. Email them as an attachment.

I have 1 and 4 completed, but how do you create an array of files in memory? When I tried the files from the database the data is in a byte[] and the filename is a string.

Any thoughts?

3 Answers3

1

When you got the files as byte arrays (or memory streams) you can use .NET ZipArchive class to write the files to a ZIP file.

class ZipFile
{
    public string Name { get; set; }
    public byte[] Data { get; set; }
}
...
var files = new List<ZipFile>(); //The files to zip
var zipStream = new MemoryStream(); //Where the zip archive is stored

using (var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Create))
{
    foreach (var file in files)
    {
        var fileEntry = zipArchive.CreateEntry(file.Name);

        using (var entryStream = fileEntry.Open())
        {
            entryStream.Write(file.Data, 0, file.Data.Length);
        }
    }
}

//You can now send the zip archive as binary data.
svenslaggare
  • 438
  • 4
  • 5
0

If I understand your question correctly, it is something like this:

class FileToZip
{
    public string fileName { get; set; }
    public byte[] data { get; set; }
}

and then

var listOfFile = new List<FileToZip>();

or

var arrayOfFile = new FileToZip[numberOfFiles];

There should be instructions with the Zip library that describe how to put this information into the zip file.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
  • Robert maybe but how do you zip that –  Jun 24 '14 at 17:31
  • Robert - soo I can iterate of my List but how do you zip each of those list[] –  Jun 24 '14 at 17:31
  • Like I said, the Zip Library you're using should have instructions that tell you how to do that. – Robert Harvey Jun 24 '14 at 17:32
  • Robert Maybe i didnt make it clear and probably didnt, but lets say I have 100mb worth of files and i want to make them 25mb chunks, dont I have to do a list of a list? –  Jun 24 '14 at 17:34
  • Robert because I already have the first List in the example you provided from the call to the database, i think this post has the missing piece http://stackoverflow.com/questions/9855633/ionic-zip-zip-file-creation-from-byte –  Jun 24 '14 at 17:39
-1

You can accomplish this with the ZipArchive class in System.IO.Compression. http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive(v=vs.110).aspx

Here is an example:

// Test data representing what you pulled from the database in #1
string file1Name = "Hello.txt";
byte[] file1Data = Encoding.UTF8.GetBytes("Hello World");


// Open a new .zip file
using (FileStream stream = new FileStream(@"my.zip", FileMode.Create))
using (ZipArchive archive = new ZipArchive(stream, ZipArchiveMode.Update))
{
    // Add each item to the zip file. Loop this if you have multiple
    ZipArchiveEntry readmeEntry = archive.CreateEntry(file1Name);
    using (BinaryWriter writer = new BinaryWriter(readmeEntry.Open()))
    {
        writer.Write(file1Data);
    }
}

This will create a file named my.zip with a file named Hello.txt inside it. My.zip will be in your output folder.

Mike Hixson
  • 5,071
  • 1
  • 19
  • 24