2011-10-28 15 views
0

私はWindows Phoneアプリケーションを持っています。 SharpZipLibを使用して、フォルダとそのサブフォルダを圧縮しています。これはフォルダのみを圧縮していますが、フォルダ内のデータは圧縮されていません。どのようにこれを行うに私を導くことができますか?Silverlightでフォルダとそのサブフォルダを解凍して解凍する方法は?

マイコード:

private void btnZip_Click(object sender, RoutedEventArgs e) 
    { 
     using (IsolatedStorageFile appStore = IsolatedStorageFile.GetUserStoreForApplication()) 
     { 
      foreach (string filename in appStore.GetFileNames(directoryName + "/" + "*.txt")) 
      {     
       GetCompressedByteArray(filename); 
      } 
      textBlock2.Text = "Created file has Zipped Successfully"; 
     } 
    } 
public byte[] GetCompressedByteArray(string content) 
     { 
      byte[] compressedResult; 
      using (MemoryStream zippedMemoryStream = new MemoryStream()) 
      { 
       using (ZipOutputStream zipOutputStream = new ZipOutputStream(zippedMemoryStream)) 
       { 
        zipOutputStream.SetLevel(9); 
        byte[] buffer;     
        using (MemoryStream file = new MemoryStream(Encoding.UTF8.GetBytes(content))) 
        { 
         buffer = new byte[file.Length]; 
         file.Read(buffer, 0, buffer.Length); 
        }      
        ZipEntry entry = new ZipEntry(content); 
        zipOutputStream.PutNextEntry(entry); 
        zipOutputStream.Write(buffer, 0, buffer.Length); 
        zipOutputStream.Finish(); 
       } 
       compressedResult = zippedMemoryStream.ToArray(); 
      } 
      WriteToIsolatedStorage(compressedResult); 
      return compressedResult; 
     } 

     public void WriteToIsolatedStorage(byte[] compressedBytes) 
     { 
      IsolatedStorageFile appStore = IsolatedStorageFile.GetUserStoreForApplication(); 
      appStore.CreateDirectory(ZipFolder); 
      using (IsolatedStorageFileStream zipTemplateStream = new IsolatedStorageFileStream(ZipFolder+"/"+directoryName + ".zip", FileMode.OpenOrCreate, appStore)) 
      using (BinaryWriter streamWriter = new BinaryWriter(zipTemplateStream)) 
      { 
       streamWriter.Write(compressedBytes); 
      } 
     } 
+0

別のライブラリhttp://dotnetzip.codeplex.com/があります。最終的にはこのライブラリで動作しますか? –

+0

SharpZipLibではできませんか? – Shri

答えて

1

私はあなたがthis guideは役に立ち見つけることだと思います。

がパラメータdirectoryNameでを受け入れること)ZIPファイルオブジェクトがするaddDirectory(と呼ばれる方法を提供

上記のリンクからの抜粋。この方法の問題は です。指定したディレクトリ内にファイルを追加しませんが、 はzipファイル内にディレクトリを作成するだけです。これを にするには、そのディレクトリ内のすべてのオブジェクトを でループして一度に1つずつ追加して、そのディレクトリ内のファイルを取得する必要があります。私は zipにしたいフォルダのディレクトリ構造全体を掘り下げる再帰関数を作成することで、このタスクを達成することができました。以下は、関数のスニペットです。

私は、フォルダがzipファイルに追加されているが、内容とサブフォルダが圧縮されていない同じ問題に直面していると思います。

これが役に立ちます。

+0

@ Abhinav - 返事をありがとう。私はこれを試してみる。 – Shri

0

SharpZipLibを使用してネストされたフォルダを含むルートフォルダを圧縮する方法のコードサンプルについては、over hereをご覧ください。