如何在c#中使用SharpZipLib在zip中添加文件

时间:2012-03-21 11:05:53

标签: c# .net sharpziplib

我正在尝试使用c#中的sharpZibLib将文件添加到现有zip中。 当运行zip获得qverwrite时,zip中的所有文件都会被删除,只有新文件以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();
        }
    }

有谁能告诉我上面代码中的问题是什么?拉链为什么会被覆盖

1 个答案:

答案 0 :(得分:1)

看看这里:

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

你需要打电话

zipFile.BeginUpdate();

//add file..

zipFile.CommitUpdate();