2009-08-31 28 views
7

đang cố gắng thêm tệp vào lưu trữ hiện có bằng cách sử dụng mã sau. Khi chạy không có lỗi hoặc ngoại lệ được hiển thị nhưng không có tệp nào được thêm vào lưu trữ. Bất kỳ ý tưởng tại sao?C# sharpziplib thêm tệp vào kho lưu trữ hiện tại

 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(); 
      } 
     } 
+0

Tôi đã cập nhật câu trả lời của mình. –

Trả lời

15

Trong DotNetZip, thêm các tập tin đến một hiện zip thực sự đơn giản và đáng tin cậy.

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

Nếu bạn muốn chỉ định đường dẫn thư mục cho tệp mới, sau đó sử dụng quá tải khác cho AddFile().

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

Nếu bạn muốn thêm tập hợp tệp, hãy sử dụng AddFiles().

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

Bạn không phải lo lắng về Close(), CloseEntry(), CommitUpdate(), Finish() hoặc bất kỳ phần nào khác.

+0

tôi bắt đầu ghét SharpZipLib, đó là quá thấp mức độ và đọc tài liệu là khó hiểu –

1

Tôi nghĩ rằng cuộc gọi Finish của bạn nên được trước gọi Close của bạn.

Cập nhật: Điều này trông giống như known bug. Có thể nó đã có thể đã được cố định - bạn sẽ cần phải kiểm tra phiên bản SharpZipLib của bạn để xem nó có kết hợp bất kỳ sửa chữa nào không. Nếu không, bạn có thể làm việc xung quanh nó bằng cách sao chép tất cả các tệp vào một tệp lưu trữ mới, thêm tệp mới, sau đó di chuyển tệp lưu trữ mới sang tên lưu trữ cũ.

+0

hi, điều đó không tạo ra bất kỳ sự khác biệt nào .. chúc mừng. – Grant

2

Từ Codeproject ai đó đã sử dụng mã này. Chỉ khác biệt là gần gũi và kết thúc otherway xung quanh và ghi phần:

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); 

này là không chính xác, kích thước là newFileStream.length khác Đọc gặp khó khăn. Bạn có một mảng và bạn làm cho nó ví dụ 10-1 là dài 9 byte, từ 0 đến 8.

Nhưng đọc sách của bạn 0-9 ...

+0

Hi PoweRoy, tôi đã sử dụng mã này chính xác và vẫn không có lỗi nhưng cũng không có tập tin :( – Grant

+0

tôi cũng đã cố gắng ZipFile zipfile = new ZipFile (archivePath) này; zipFile.BeginUpdate(); ZipEntry entry = new ZipEntry (fileName); zipFile.Add (fileName); zipFile.CommitUpdate();.. nhưng sau đó những con đường đều sai tôi cần phải đặt đường dẫn tương đối – Grant

1
/// <summary> 
    /// 添加压缩文件 p 为客户端传回来的文件/夹列表,用分号隔开,不包括主路径, zipfile压缩包的名称 
    /// </summary> 
    /// <param name="p"></param> 
    /// <param name="zipfile"></param> 
    public void AddZipFile(string p, string zipfile) 
    { 
     if (ServerDir.LastIndexOf(@"\") != ServerDir.Length - 1) 
     { 
      ServerDir += @"\"; 
     } 
     string[] tmp = p.Split(new char[] { ';' }); //分离文件列表 
     if (zipfile != "") //压缩包名称不为空 
     { 
      string zipfilepath=ServerDir + zipfile; 
      if (_ZipOutputStream == null) 
      { 
       _ZipOutputStream = new ZipOutputStream(File.Create(zipfilepath)); 
      } 
      for (int i = 0; i < tmp.Length; i++) 
      { 
       if (tmp[i] != "") //分离出来的文件名不为空 
       { 
        this.AddZipEntry(tmp[i], _ZipOutputStream, out _ZipOutputStream); //向压缩文件流加入内容 
       } 
      } 
     } 
    } 
    private static ZipOutputStream _ZipOutputStream; 
    public void Close() 
    { 
     _ZipOutputStream.Finish(); 
     _ZipOutputStream.Close(); 
    } 
-1

Tôi đã tìm thấy một giải pháp đơn giản giữ nó để ZipFile và ZipEntry chỉ

 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

Sai thư viện Fahar. – jpierson

0

có một thư mục ZippedFolder trong thư mục gốc của trang web, bên trong nó chúng ta có một kho lưu trữ MyZipFiles.

Có một thư mục có tên siteImages bao gồm tất cả các tệp hình ảnh. Sau đây là mã định dạng zip để những hình ảnh

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); 
} 

nếu chúng ta có định dạng tập tin khác nhau và chúng tôi muốn tập tin của bạn được lưu trong thư mục tương ứng, bạn có thể chỉ định các mã như sau.

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); 
} 

tại các kho lưu trữ có chứa hai thư mục hình ảnh ----> img1.jpg, img2, .jpg và thư mục khác file -> customer.pdf, sample.doc

0

Lớp ZipOutputStream không cập nhật các tệp ZIP hiện có. Sử dụng lớp ZipFile để thay thế.

Các vấn đề liên quan