ZipOutputStream append data file to Exist Entry?

Apr 30, 2012 at 4:39 PM

ZipOutputStream append data file to Exist Entry?

Good day, asp.net c#, Framework 4.0 

Im using Ionic.Zip for compress data file (uploader handler) to fly.

Upload large files.

If file uploaded  full content (not chunks) - to be Ok, but if file upload with chunks...i have to problem.

How i can add all chunks upload file to archive (one file - one entry)? without saving the file itself drive.

sorry for my english.

parts of my code:

Create temp file with lenght = currents chunk

 using (FileStream fs = new FileStream(Path.Combine(pathToCreate, "chunkfile"), FileMode.Create))
               
{
                    buffer
= new byte[fileUpload.InputStream.Length];
                    fileUpload
.InputStream.Read(buffer, 0, buffer.Length);
                    fs
.Write(buffer, 0, buffer.Length);
               
}

Get zip entry:

using (ZipFile zipfile = ZipFile.Read(fullpath))
{
if (zipfile.ContainsEntry(fileName))
{
zipflag
= true;
}
}
If Entry exist - append data, if not, create new:
 if (!zipflag)
               
{
                   
using (ZipFile zipfile = ZipFile.Read(fullpath))
                   
{
                           
using ( FileStream input = new FileStream (Path.Combine(pathToCreate, "chunkfile"), FileMode.Open, FileAccess.Read))
                           
{
                               
using (FileStream output = new FileStream(fullpath, FileMode.Create, FileAccess.Write))
                               
{
                                   
using (ZipOutputStream zipout = new ZipOutputStream(output))
                                   
{
                                        buffer
= new byte[input.Length];
                                        zipout
.PutNextEntry(fileName);
                                       
int size;
                                       
do
                                       
{
                                            size
= input.Read(buffer, 0, buffer.Length);
                                            zipout
.Write(buffer, 0, size);
                                       
} while (size > 0);
                                   
}
                               
}
                           
}
                   
}
               
}
               
else
               
{
                   
                        buffer
= FileArray(Path.Combine(pathToCreate, "chunkfile"));
                       
AppendAllBytes(fullpath, buffer);
                 
}
           
}

 

public static void AppendAllBytes(string path, byte[] bytes)
{
using (var stream = new FileStream(path, FileMode.Append))
{
using (ZipOutputStream zipout = new ZipOutputStream(path))
{
zipout
.Write(bytes, 0, bytes.Length);
}
}
}
private byte[] FileArray(string filename)
{
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

// Create a byte array of file stream length
byte[] datachunk = new byte[fs.Length];

//Read block of bytes from stream into the byte array
fs
.Read(datachunk, 0, System.Convert.ToInt32(fs.Length));

//Close the File Stream
fs
.Close();
return datachunk; //return the byte data
}

variable <fullpath> this string, full path to a Zip file.

and im trying use:

using (var stream = new FileStream(path, FileMode.Append))
       
{
           
ZipFile zipfile = ZipFile.Read(path);
           
using (StreamReader sr = new StreamReader(path))
           
{
               
var zn = zipfile.UpdateEntry(fileName, bytes);
                sr
.Close();
                sr
.Dispose();
           
}
       
}

NOT effect, file not update.

Please HELP.


 

 

Coordinator
May 18, 2012 at 11:26 PM

I don't understand.

I don't know what you're asking or how to answer this question.

 

Jul 31, 2012 at 10:38 AM

ok. I want create zip with content many files.

Every file upload over http to the web site and put to the folder, file upload with XHR2 (html5 with chunks) 

and so..

my question:

So, upload file - image1.jpg

When upload first chunk (image1.jpg) creating temporary file (tempfile) with file.lenght = chunk.lenght, next

create zip file (allfiles.zip) with content this file (image1.jpg  - 1 chunk), next

upload next chunk, open allfiles.zip, open image1.jpg and adds the following chunk, update  allfiles.zip, and so on until the file (image1.jpg) is fully loaded, 

then the next file is loaded.....

How to do this ?????