riya
riya

Reputation: 125

How to add a file in a zip using SharpZibLib in c#

I am trying to add a file to an existing zip using sharpZibLib in c#. When run zip gets qverwrite i.e all files within zip gets deleted and only new file is there in a zip.

using (FileStream fileStream = File.Open("D:/Work/Check.zip", FileMode.Open, FileAccess.ReadWrite))
    using (ZipOutputStream zipToWrite = new ZipOutputStream(fileStream))
    {
        zipToWrite.SetLevel(9);

        using (FileStream newFileStream = File.OpenRead("D:/Work/file1.txt"))
        {
            byte[] byteBuffer = new byte[newFileStream.Length - 1];

            newFileStream.Read(byteBuffer, 0, byteBuffer.Length);

            ZipEntry entry = new ZipEntry("file1.txt");
            zipToWrite.PutNextEntry(entry);
            zipToWrite.Write(byteBuffer, 0, byteBuffer.Length);
            zipToWrite.CloseEntry();


            zipToWrite.Finish();
            zipToWrite.Close();
        }
    }

Can anyone tell me whats the issue in above code? Why the zip gets overwitten

Upvotes: 0

Views: 5090

Answers (1)

Steve
Steve

Reputation: 1082

Have a look here:

http://wiki.sharpdevelop.net/SharpZipLib_Updating.ashx

you need to call

zipFile.BeginUpdate();

//add file..

zipFile.CommitUpdate();

Upvotes: 1

Related Questions