View Single Post
Old 27-09-2007, 03:32 AM   #3
Chris Fink
Guest
 
Posts: n/a
Re: Zip file using a stream

Jon,

Thank You for your advice. I've switched to the ZipLib and have a few issues.
#1 The last file is not being added to the ZipOutputStream entries
#2 When I test the zip file by writing the stream to disk, it is 0 length,
but valid.

Any assistance you can provide is appreciated

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;

namespace testzipper
{
class Program
{
static void Main(string[] args)
{

string[] files = new string[] { "c:\\temp\\test1.txt",
"c:\\temp\\test2.txt", "c:\\temp\\test5.pdf", "c:\\temp\\test6.doc" };
ZipToMemory("test.zip", files);
}

// this method outputs a zip file to memory
private static void ZipToMemory(string zipFileName, string[]
filenames)
{
try
{

System.IO.MemoryStream ms = new MemoryStream();
using (ZipOutputStream s = new ZipOutputStream(ms))
{

s.SetLevel(9); // 0 - store only to 9 - means best
compression
byte[] buffer = new byte[4096];
foreach (string file in filenames)
{
ZipEntry entry = new ZipEntry(file);
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
// TODO: last file is not added as entry
byte[] ba = GetBlob(13);
s.Write(ba, 0, ba.Length);
}

// TODO: write stream to BLOB
//...But before I try that test that the zip is valid by
writting the ZipOutputStream
// to disk
using (FileStream fs = File.OpenRead(zipFileName))
{
int sourceBytes;
do
{
sourceBytes = fs.Read(buffer, 0, buffer.Length);
s.Write(buffer, 0, sourceBytes);
} while (sourceBytes > 0);
}

s.Finish();
s.Close();
}

}
catch (Exception ex)
{
Console.WriteLine("Exception during processing {0}", ex);

}
}

private static byte[] GetBlob(int id)
{
byte[] data = null;
string fileName = string.Empty;
string cn = "xxxxxxxx";
using (SqlConnection mySqlConnection = new SqlConnection(cn))
{
SqlCommand myCommand = new SqlCommand("BN_GetData",
mySqlConnection);
myCommand.CommandType = CommandType.StoredProcedure;

SqlParameter prmId = new SqlParameter("@ID", SqlDbType.Int);
prmId.Value = id;
myCommand.Parameters.Add(prmId);

mySqlConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read())
{
//
data =
(byte[])myReader[myReader.GetOrdinal("PDFData")];
fileName = (string)myReader["FileName"];
}
myReader.Close();
}
mySqlConnection.Close();
}
return data;
}
}
}


"Jon Skeet [C# MVP]" wrote:

> Chris Fink <ChrisFink@discussions.microsoft.com> wrote:
> > I am trying to make a minor modification to the code below and need some
> > assistance. Currently this code is using the java.util, java.util.zip, and
> > java.io assemblies from the vjslib.dll assembly. This code works fine by
> > taking file(s) from
> > disk and creating a zip file to disk. I need to modify this code to read a
> > file from a byte array and then output the zip file to a byte array (all done
> > in memory instead of disk). The reason for the byte array is that all the
> > files are being stored in the database in a BLOB field. Any assistance is
> > appreciated!

>
> Use a MemoryOutputStream instead of a FileOutputStream. You can do this
> without using the Java classes at all using #ZipLib (google SharpZipLib
> for details) and MemoryStream.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>

  Reply With Quote