View Single Post
Old 02-10-2007, 08:38 PM   #9
Chris Fink
Guest
 
Posts: n/a
Re: Zip file using a stream

Attached is my code that i cleaned up a bit. My previous issues still exist,
however, I am focused at figuring out why the zip file is corrupt in cases #1
and #2. Case #2 should be easy to recreate, just change the path to point to
a pdf on your system. I've added comments to assist you along the way.

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" };
StreamZipper("test.zip");
}

// This method outputs a zip file to a memorystream
// The input in all three test cases is a byte array from different
sources
// (This will create a zip file with 1 file)
private static void StreamZipper(string zipFileName) // zipFileName
is not used, it is hardcoded for testing
{
try
{
// test case #1: read a BLOB (from the database) into a byte
array to create zip file
// the byte array is fine, however the zip file created is
corrupt.
// this byte array is valid when output as a file type
//byte[] ba = GetBlob(13);
// end test case #1

// test case #2: read the valid blob above from it's PDF
FILE (from disk) directly into the byte[]
// again...the zip file created is corrupt
// the pdf is valid
//FileInfo fInfo = new FileInfo("c:\\temp\\test5.pdf");
//long numBytes = fInfo.Length;
//FileStream fStream = new FileStream("c:\\temp\\test5.pdf",
FileMode.Open, FileAccess.Read);
//BinaryReader br = new BinaryReader(fStream);
//byte[] ba = br.ReadBytes((int)numBytes);
//string len = Convert.ToString(ba.Length);
//br.Close();
//fStream.Close();
// end test case #2

// test case #3: create a byte array from a string then
create a zip file
// this works, the zip file is valid..not sure why it shows
the zip file size as 4 million KB...but it works
byte[] ba = Encoding.Default.GetBytes("TEST");
// end test case #3

// create the zip file in memory
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];

// you'll need to switch these depending on the case#
you are running...sorry
ZipEntry entry = new ZipEntry("file1.txt"); // for test
case #3
//ZipEntry entry = new ZipEntry("file1.pdf"); // for
test case #1 and #2
entry.DateTime = DateTime.Now;
s.PutNextEntry(entry);
s.Write(ba, 0, ba.Length);
s.Finish();
s.Close();
}

// ms is the memory stream that contains the zip file that I
will insert into a BLOB.
// let's test that the ms is valid by writting it out to a
zip file first.
byte[] zipms = (byte[])ms.ToArray();
using (BinaryWriter binWriter = new
BinaryWriter(File.Open(@"C:\\temp\\test.zip", FileMode.Create)))
{
//byte[] bb = new byte[] { 65, 66, 67 };

// write the zip file to disk from the memorystream
binWriter.Write(zipms);
binWriter.Write(ba);
}

}
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 = "Server=localhost\\SQLEXPRESS;Initial
Catalog=XXX;user id=xxx; Password=xxx;";
using (SqlConnection mySqlConnection = new SqlConnection(cn))
{
SqlCommand myCommand = new SqlCommand("SPNAME1",
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;
}
}
}




"Peter Duniho" wrote:

> Chris Fink wrote:
> > [...]
> > So what i need help with is
> > 1. figuring out why the last item is not added to the list

>
> I don't know the answer to this. You might want to contact the author
> of the class you're using. However, the fact that you could potentially
> write additional junk to the ZipOutputStream at the end might corrupt
> that last entry, causing it to not be seen later. I wouldn't think a
> zero-length file would cause this problem, but I suppose you never know.
> Depends on how well the ZipOutputStream handles zero-length writes (it
> should handle them fine, but all code has bugs).
>
> > 2. most importantly, why the zip file that is written to disk is empty.

>
> You haven't posted any code that writes to a disk file, so it's not
> really possible to answer this question. Though, if you've posted all
> of the code you have, then the lack of any code that would do that is
> likely the answer itself.
>
> You should look more closely at the section of the code that I described
> as confusing. It reads from a file and writes the data to the
> ZipOutputStream, which seems to be the opposite of what you say you want.
>
> Pete
>

  Reply With Quote