c# sharpziplib adding file to existing archive

前端 未结 7 1173
-上瘾入骨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:10

    In DotNetZip, adding files to an existing zip is really simple and reliable.

    using (var zip = ZipFile.Read(nameOfExistingZip))
    {
        zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
        zip.AddFile(additionalFileToAdd);
        zip.Save();
    }
    

    If you want to specify a directory path for that new file, then use a different overload for AddFile().

    using (var zip = ZipFile.Read(nameOfExistingZip))
    {
        zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
        zip.AddFile(additionalFileToAdd, "directory\\For\\The\\Added\\File");
        zip.Save();
    }
    

    If you want to add a set of files, use AddFiles().

    using (var zip = ZipFile.Read(nameOfExistingZip))
    {
        zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
        zip.AddFiles(listOfFilesToAdd, "directory\\For\\The\\Added\\Files");
        zip.Save();
    }
    

    You don't have to worry about Close(), CloseEntry(), CommitUpdate(), Finish() or any of that other gunk.

    0 讨论(0)
  • 2020-12-11 18:15

    The ZipOutputStream class does not update existing ZIP files. Use the ZipFile class instead.

    0 讨论(0)
  • 2020-12-11 18:18

    I have found a simple solution keeping it to ZipFile and ZipEntry only

            ZipFile zipExisting = ZipFile.Read(Server.MapPath("/_Layouts/includes/Template.zip"));
            ICollection<ZipEntry> entries = _zipFileNew.Entries;
            foreach (ZipEntry zipfile in entries)
            {
                zipExisting.AddEntry(zipfile.FileName, zipfile.InputStream);
            } 
    
            zipExisting.Save(Response.OutputStream);
            Response.End();
    
    0 讨论(0)
  • 2020-12-11 18:28

    I think your Finish call should be before your Close 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.

    0 讨论(0)
  • 2020-12-11 18:32

    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

    string zipPath = Server.MapPath("~/ZippedFolder/MyZipFiles.zip");
    using (ZipFile zip = new ZipFile())
    {
     zip.AddFile(Server.MapPath("~/siteImages/img1.jpg"),string.Empty);
     zip.AddFile(Server.MapPath("~/siteImages/img2.jpg"),string.Empty);
     zip.AddFile(Server.MapPath("~/siteImages/img2.jpg"),string.Empty);
     zip.Save(zipPath);
    }
    

    if we have different file formats and we want your files to be saved in respective folders,you can specify the code as follows.

    string zipPath = Server.MapPath("~/ZippedFolder/MyZipFiles.zip");
    using (ZipFile zip = new ZipFile())
    {
      zip.AddFile(Server.MapPath("~/siteimages/img1.jpg"), "images");
      zip.AddFile(Server.MapPath("~/siteimages/img2.jpg"), "images");
      zip.AddFile(Server.MapPath("~/documents/customer.pdf"), "files");
      zip.AddFile(Server.MapPath("~/documents/sample.doc"), "files");
      zip.Save(zipPath);
    }
    

    now the archive contains two folders images ---- > img1.jpg , img2,.jpg and another folder files --> customer.pdf, sample.doc

    0 讨论(0)
  • 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...

    0 讨论(0)
提交回复
热议问题