am trying to add a file to an existing archive using the following code. When run no errors or exceptions are shown but no files are added to the archive either. Any ideas why?
using (FileStream fileStream = File.Open(archivePath, FileMode.Open, FileAccess.ReadWrite))
using (ZipOutputStream zipToWrite = new ZipOutputStream(fileStream))
{
zipToWrite.SetLevel(9);
using (FileStream newFileStream = File.OpenRead(sourceFiles[0]))
{
byte[] byteBuffer = new byte[newFileStream.Length - 1];
newFileStream.Read(byteBuffer, 0, byteBuffer.Length);
ZipEntry entry = new ZipEntry(sourceFiles[0]);
zipToWrite.PutNextEntry(entry);
zipToWrite.Write(byteBuffer, 0, byteBuffer.Length);
zipToWrite.CloseEntry();
zipToWrite.Close();
zipToWrite.Finish();
}
}
In DotNetZip, adding files to an existing zip is really simple and reliable.
If you want to specify a directory path for that new file, then use a different overload for AddFile().
If you want to add a set of files, use AddFiles().
You don't have to worry about Close(), CloseEntry(), CommitUpdate(), Finish() or any of that other gunk.
I have found a simple solution keeping it to ZipFile and ZipEntry only
I think your
Finish
call should be before yourClose
call.Update: This looks like a known bug. It's possible it may already have been fixed - you'll need to check your SharpZipLib version to see if it incorporates any fix. If not, you can work around it by copying all files to a new archive, adding the new file, then moving the new archive to the old archive name.
The
ZipOutputStream
class does not update existing ZIP files. Use theZipFile
class instead.there is a folder ZippedFolder in site's root directory , inside it we have a archive MyZipFiles.
There is a folder with name siteImages which consists of all image files. The following is the code to zip the images
if we have different file formats and we want your files to be saved in respective folders,you can specify the code as follows.
now the archive contains two folders images ---- > img1.jpg , img2,.jpg and another folder files --> customer.pdf, sample.doc