SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

How to create zip file from the file contents in bytes from database

Last post 11-20-2008 11:01 PM by clamum. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 09-29-2008 3:55 PM

    How to create zip file from the file contents in bytes from database

    Hi,

    i have an application where i need to make a zip file from file contents stored in SQL server database. i am getting each file in bytes as per the below code

    using (SqlDataReader cvReader = _command.ExecuteReader())

    {

    using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFile)))

    {

    s.SetLevel(0); // 0 - store only to 9 - means best compression

    while (cvReader.Read())

    {

    bytes = Utility.GetBytes(cvReader, 4, 3);

    ZipEntry entry = new ZipEntry(file);

    // using the above bytes, need to create a file entry (not physical file) by giving filename to it and add it to ZipEntry ? help me

    s.PutNextEntry(entry);

    //Copy the bytes to the output stream

    ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(fs, s, buffer);

    }

    }

    }

    check the comment, where i need some help to to complete it.

    Please give me the sollution / code or anything helpfull ASAP.

  • 10-06-2008 8:27 PM In reply to

    Re: How to create zip file from the file contents in bytes from database

    // Something like this should work I believe.

    // Each entry should have a unique name

    ZipEntry entry = new ZipEntry(cvReader.Current.Name); // How to identify each cv?

    s.PutNextEntry(entry);

    s.Write(buffer, 0, buffer.Length);
  • 11-20-2008 11:01 PM In reply to

    • clamum
    • Not Ranked
    • Joined on 11-20-2008
    • Michigan, USA
    • Posts 3

    Re: How to create zip file from the file contents in bytes from database

    I am wondering how to do the same thing as the original poster is. I know how to do the actual zipping in memory but I do not know how (or if it is possible) to add files, in the form of byte array, to the zip in memory.

    From what I understand, the process is as follows:

    1. Create a ZipOutputStream.
    2. Create a ZipEntry and pass it the path of the file to add.
    3. Call ZipOutputStream.PutNextEntry(ZipEntry).
    4. Close streams, etc.

    My question is this: Can I somehow create a ZipEntry with an array of bytes, instead of a file path? Otherwise, I cannot see how you could do this in memory. I tried out the ZipOutputStream.Read() method which takes in an array of bytes but I receive a "DeflaterOutputStream Read not supported" exception.

    I am using assembly version 0.85.5.452 of the ICSharpCode.SharpZipLib library with .NET Framework 3.5.

    EDIT: Found the solution (from a reply in another thread). Basically the ZipEntry is created with the filename as the string (which I thought needed to be an actual path to the file) and the MemoryStream's WriteTo() method is used and the ZipOutputStream is sent as an argument:

    ZipEntry zipEntry = new ZipEntry("filename");
    zipOutputStream.PutNextEntry(entry);
    fileMemoryStream.WriteTo(zos)

Page 1 of 1 (3 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.