2014-10-31 108 views
2

我得到了一個場景,我需要遞歸解壓縮文件夾。 我已經通過了幾個鏈接,但不符合我的要求。我使用單聲道3.2.6,需要在android xamarin中遞歸提取。在Android Xamarin單聲道中遞歸解壓縮文件夾3.2.6

我使用了第三方工具ICSharpCode.SharpZipLib.Zip。這在我的應用程序的調試模式下工作正常。但在釋放模式下引發錯誤。在真實設備上安裝APK之後。

即使嘗試Dotnet.Zip但我無法遞歸解壓縮文件夾。 here建議開源DLL但它集中在文件中,而然後夾

另一個link其示出了在Java機器人的解決方案,我正在尋找在C#xamarin機器人相同的類型。 有沒有辦法做到這一點,即使免費的第三方工具也沒關係。任何人都可以向我提出解決方案或提示。

回答

5

如果在釋放模式下拋出錯誤,則可能是因爲鏈接器。爲了壓縮和解壓縮文件,你也可以使用

java.util.zip

包。更多信息here

編輯:示例代碼

包括命名空間using Java.Util.Zip;

using (ZipInputStream s = new ZipInputStream (File.OpenRead (strSourcePath))) 
{ 
    ZipEntry theEntry; 
    while ((theEntry = s.NextEntry) != null) 
    { 
     string directoryName = Path.GetDirectoryName (theEntry.Name); 
     string fileName = Path.GetFileName (theEntry.Name); 
     directoryName = Path.Combine (strDestFolderPath , directoryName); 
     if (directoryName.Length > 0) 
     { 
      Directory.CreateDirectory (directoryName); 
     } 
     if (fileName != String.Empty) 
     { 
      using (FileStream streamWriter = File.Create (Path.Combine (strDestFolderPath , theEntry.Name))) 
      { 
       int size = 2048; 
       byte [] data = new byte[size]; 
       while (true) 
       { 
        size = s.Read (data , 0 , data.Length); 
        if (size > 0) 
        { 
         streamWriter.Write (data , 0 , size); 
        } 
        else 
        { 
         break; 
        } 
       } 
      } 
     } 
    } 
} 

其中strSourcePath是源zip文件的路徑和strDestFolderPath是目標文件夾路徑

+1

這對我有用,另一個解決方案是添加更改鏈接器內部化設置爲「西部」選項。 Android->建設 - >連接器 - >內在東西 – Suchith 2014-11-04 08:41:51

0

您也可以使用System.IO.Compression命名空間。

public static async Task ExtractToDirectory(string strSourcePath, string strDestFolderPath, 
     IProgress<int> progessReporter) 
    { 

     await Task.Factory.StartNew(() => 
     { 

      using (ZipArchive archive = new ZipArchive(File.Open(strSourcePath, FileMode.Open))) 
      { 
       double zipEntriesExtracted = 0; 
       double zipEntries; 

       zipEntries = archive.Entries.Count; 

       foreach (ZipArchiveEntry entry in archive.Entries) 
       { 
        try 
        { 
         string fullPath = Path.Combine(strDestFolderPath, entry.FullName); 
         if (String.IsNullOrEmpty(entry.Name)) 
         { 
          Directory.CreateDirectory(fullPath); 
         } 
         else 
         { 
          var destFileName = Path.Combine(strDestFolderPath, entry.FullName); 

          using (var fileStream = File.Create(destFileName)) 
          { 
           using (var entryStream = entry.Open()) 
           { 
            entryStream.CopyTo(fileStream); 
           } 
          } 
         } 

         zipEntriesExtracted++; 
         progessReporter.Report((int)((zipEntriesExtracted/zipEntries) * 100)); 
        } 
        catch (Exception ex) 
        { 

        } 
       } 
      } 

     }, TaskCreationOptions.LongRunning); 


    }