2016-02-18 64 views
0

在PCL中,我使用SharpZipLib.Portable將一些內容壓縮成MemoryStream,然後使用PCLStorage的二進制寫入工具將MemoryStream寫入Zip文件。但是,我創建的zip文件已損壞。關於上市的內容我得到的消息:將SharpZipLib內存流傳遞給PclStorage時損壞的壓縮文件

存檔:在開始或zip文件
內(試圖反正處理)
錯誤[zippedfile 20個額外的字節:zippedfile.zip
警告[zippedfile.zip。 zip]:找不到中央目錄的開頭;
zipfile損壞。
(請檢查您是否已經轉移或創造了
相應的二進制模式zip文件,並且已經編譯解壓正確)

的代碼如下。任何人都可以提出我要去哪裏?我無法想象SharpZipLib或PCLStorage有什麼問題,但是有一個奇怪的異常:

[編輯:下面描述的異常的一些新的亮點:每個大於7F的字節的出現被翻譯到EF BF BD字節的三倍,這似乎是問號字符風格的UTF8多字節表示。所以看起來PCLStorage將流視爲UTF8。那麼問題似乎是,如何說服PCLStorage進行二進制傳輸。 ]

雖然料流進入PCLStorage,我在斷點處檢查流(「驗證」變量),並看到該六角內容是振振有詞類似於ZIP格式,例如具有一個正確的郵政編碼頭:

50 4B 03 04 14 00 00 00 08 00 45 89 52 48 DF 36 ...(122字節)

當我在十六進制編輯器中查看創建的zip文件時,我還看到一些振振有詞類似於ZIP格式,但在文件正文中是不同! :

50 4B 03 04 14 00 00 00 08 00 45 BF EF BD 52 48(142個字節)

在這兩種情況下,ASCII等效的形式爲:

PK .. ..... ABCD.txt .... PK ..... ABCD.txt ..... PK .....

與fileHandler.WriteAsync代替.CopyTo使用代碼時的結果類似。

其他人使用PCLStorage二進制傳輸,所以我懷疑問題在那裏。我錯過了什麼?歡迎任何建議。

這是Xamarin Forms PCL。

// using ICSharpCode.SharpZipLib.Zip; // SharpZipLib.Portable 
// using PCLStorage; 

string content = "ABCD\r\n";   // Desired content of zipped file 
byte[] contentBytes = Encoding.UTF8.GetBytes (content); 

using (MemoryStream contentStream = new MemoryStream()) 
{ 
    await contentStream.WriteAsync (contentBytes, 0, contentBytes.Length); 
    contentStream.Position = 0; 

    using (MemoryStream zipStream = new MemoryStream()) 
    { 
     using (ZipOutputStream s = new ZipOutputStream (zipStream)) 
     { 
      s.UseZip64 = UseZip64.Off; 
      s.SetLevel (6);    // Compression level 

      //Add the text file 
      ZipEntry csvEntry = new ZipEntry ("ABCD.txt"); 
      s.PutNextEntry (csvEntry); 
      await contentStream.CopyToAsync (s); 
      s.CloseEntry(); 

      s.IsStreamOwner = false; // Do not close zipStream when finishing 
      await s.FlushAsync();  // Write to zipStream 
      s.Finish(); 
     } 

     // Save file to local file system 
     IFolder rootfolder = FileSystem.Current.LocalStorage; 
     IFolder exportfolder = await rootfolder.CreateFolderAsync ("Exports", CreationCollisionOption.OpenIfExists); 
     IFolder subfolder = await exportfolder.CreateFolderAsync ("Zips", CreationCollisionOption.OpenIfExists); 
     IFile file = await subfolder.CreateFileAsync ("ZippedFile.zip", CreationCollisionOption.ReplaceExisting); 
     using (Stream fileHandler = await file.OpenAsync (FileAccess.ReadAndWrite)) 
     { 
      zipStream.Position = 0; 
      await zipStream.CopyToAsync (fileHandler); 

      // As a sanity check, view the contents of fileHandler 
      using (MemoryStream memStream = new MemoryStream()) 
      { 
       fileHandler.Position = 0; 
       fileHandler.CopyTo (memStream); 
       byte[] verify = memStream.ToArray(); 
      } // Put breakpoint here to view contents of verify 
     } 
    } 

回答

0

對不起,夥計們。我的錯!寫入的代碼正確運行並在文件系統中生成有效的ZIP文件。我的測試用具代碼是問題。

- Bill。

+0

我正在'System.NotImplementedException:此功能未在此程序集的可移植版本中實現。您應該從您的主應用程序項目引用PCLStorage NuGet軟件包以引用特定於平臺的實現。'儘管我已經將PCLStorage Nuget包添加到pcl,android和ios項目中,但仍出現錯誤 – Onur

+0

您是否嘗試使用PCLStorage在不使用zip庫的情況下執行簡單的文件操作? – BillF

+0

是的,我試過了,得到了同樣的結果。然後我創建了另一個項目並複製粘貼所有文件。現在它可以工作,但我仍然不知道錯誤的原因 – Onur