2016-05-16 8 views
1

私はUWPとWindows phone 8.1を同じソリューションで開発しています。PCLからgzip/zipファイルへのフォルダを圧縮

両方のプロジェクトで、フォルダ全体を1つのgzipファイル(サーバーに送信するため)に圧縮する機能が必要です。

PCL/UWPをSuportingない - 私は私のPCLプロジェクト

DotNetZipにreferanceすることはできませんSystem.IClonableを使用しています -

SharpZipLibは:私が試したとの問題が発生しました

ライブラリ

System.IO.Compression - ストリームのみで動作し、フォルダ全体を圧縮できない

私は各プラットフォームの実装を分割できますが(完全ではありませんが)、UWPで使用できるものはまだ見つかりませんでした。

すべてのヘルプはあなたがSystem.IO.Compressionのストリーム・サブシステムを使用する必要がありますUWPライブラリで作業

+0

説明できますか?あなたがなぜIO.Compressionを使うことができなかったのですか?私はZipFileライブラリでCreateFromDirectoryを使用することに問題は一度もありませんでしたが、私はUWPやWPアプリケーションも書いていないので、それはおそらく問題ですか?私はまだいないよ、彼らはすべてのストリームでのみ動作します... – gravity

+0

ああ、私はちょうど私が持っている、IO.Compression名前空間にZIPファイルを持っていけない、私は –

+0

Apperenltyを試してみましょう:) CreateFromDirectoryを見逃している必要がありますクリア、UWP/WPのアプリはストリームを使用することはできません? –

答えて

0

[OK]を、ので、私はまた、オープンソース GithubにあるSharpZipLib.Portableと呼ばれるこのプロジェクトを見つけました:

必要usingsはhttps://github.com/ygrenier/SharpZipLib.Portable

本当にnice :)

+0

ファイルとの間で読み込みを行っていない場合にはうまく動作しません – Dagrooms

-1

をにappriciatedされます。 .NET FrameworkのPCLバージョンが必要な場合は、多くの制限があります。それで生きる

あなたの文脈では、それほど問題ではありません。

その後
using System; 
using System.IO; 
using System.IO.Compression; 

方法...

private void CreateArchive(string iArchiveRoot) 
    { 
     using (MemoryStream outputStream = new MemoryStream()) 
     { 
      using (ZipArchive archive = new ZipArchive(outputStream, ZipArchiveMode.Create, true)) 
      { 
       //Pick all the files you need in the archive. 
       string[] files = Directory.GetFiles(iArchiveRoot, "*", SearchOption.AllDirectories); 

       foreach (string filePath in files) 
       { 
        FileAppend(iArchiveRoot, filePath, archive); 
       } 
      } 
     } 
    } 

    private void FileAppend(
     string iArchiveRootPath, 
     string iFileAbsolutePath, 
     ZipArchive iArchive) 
    { 
     //Has to return something like "dir1/dir2/part1.txt". 
     string fileRelativePath = MakeRelativePath(iFileAbsolutePath, iArchiveRootPath); 

     ZipArchiveEntry clsEntry = iArchive.CreateEntry(fileRelativePath, CompressionLevel.Optimal); 
     Stream entryData = clsEntry.Open(); 

     //Write the file data to the ZipArchiveEntry. 
     entryData.Write(...); 
    } 

    //http://stackoverflow.com/questions/275689/how-to-get-relative-path-from-absolute-path 
    private string MakeRelativePath(
     string fromPath, 
     string toPath) 
    { 
     if (String.IsNullOrEmpty(fromPath)) throw new ArgumentNullException("fromPath"); 
     if (String.IsNullOrEmpty(toPath)) throw new ArgumentNullException("toPath"); 

     Uri fromUri = new Uri(fromPath); 
     Uri toUri = new Uri(toPath); 

     if (fromUri.Scheme != toUri.Scheme) { return toPath; } // path can't be made relative. 

     Uri relativeUri = fromUri.MakeRelativeUri(toUri); 
     String relativePath = Uri.UnescapeDataString(relativeUri.ToString()); 

     if (toUri.Scheme.Equals("file", StringComparison.OrdinalIgnoreCase)) 
     { 
      relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); 
     } 

     return relativePath; 
    } 
+0

私は似たようなものを使用しようとしましたが、通常のgzipファイル(Linuxサーバ上では例えばuzipできます)をそのまま作成しませんでした。 Stream(clsEntry)に追加したファイルを含むフォルダとして解凍されるgzip形式のファイルを作成できましたか? –

+0

私はDotNetZip(https://dotnetzip.codeplex.com/)をWindows 8.1/Windows Phone 8.1用のPCLとして大きな問題なく再コンパイルできました。約1時間かかりましたが、まだ試してみませんでした。私がしなければならなかった仕事の大部分は、ファイルシステムコールを置き換えることです。 –

関連する問題