c# sharpziplib adding file to existing archive

前端 未结 7 1217
-上瘾入骨i
-上瘾入骨i 2020-12-11 17:47

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 w

7条回答
  •  误落风尘
    2020-12-11 18:37

    From Codeproject someone used this code. Only difference is close and finish otherway around and the write part:

    using (ZipOutputStream s = new
    ZipOutputStream(File.Create(txtSaveTo.Text + "\\" +
    sZipFileName + ".zip")))
    {
        s.SetLevel(9); // 0-9, 9 being the highest compression
    
        byte[] buffer = new byte[4096];
    
        foreach (string file in filenames)
        {
    
            ZipEntry entry = new
            ZipEntry(Path.GetFileName(file));
    
            entry.DateTime = DateTime.Now;
            s.PutNextEntry(entry);
    
            using (FileStream fs = File.OpenRead(file))
            {
                int sourceBytes;
                do
                {
                    sourceBytes = fs.Read(buffer, 0,
                    buffer.Length);
    
                   s.Write(buffer, 0, sourceBytes);
    
                } while (sourceBytes > 0);
            }
        }
        s.Finish();
        s.Close();
    }
    

    BTW:

    byte[] byteBuffer = new byte[newFileStream.Length - 1];
    
                    newFileStream.Read(byteBuffer, 0, byteBuffer.Length);
    

    This is incorrect, the size is newFileStream.length else the Read goes wrong. You have an array and you make it for example 10-1 is 9 bytes long, from 0 to 8.

    But your reading from 0 to 9...

提交回复
热议问题