C# 将文件夹从PCL压缩为gzip/zip文件

C# 将文件夹从PCL压缩为gzip/zip文件,c#,zip,win-universal-app,gzip,portable-class-library,C#,Zip,Win Universal App,Gzip,Portable Class Library,我正在同一个解决方案中开发UWP和Windows phone 8.1 在这两个项目中,我都需要将整个文件夹压缩为一个gzip文件的功能(以便将其发送到服务器) 我尝试过的库遇到了以下问题: SharpZipLib-使用System.IClonable,我无法在PCL项目中引用它 DotNetZip-不支持PCL/UWP System.IO.Compression-仅适用于流,无法压缩整个文件夹 我可以为每个平台分割实现(尽管它并不完美),但我仍然没有找到可以在UWP中使用的东西 在UWP库上工作

我正在同一个解决方案中开发UWP和Windows phone 8.1

在这两个项目中,我都需要将整个文件夹压缩为一个gzip文件的功能(以便将其发送到服务器)

我尝试过的库遇到了以下问题:

SharpZipLib-使用System.IClonable,我无法在PCL项目中引用它

DotNetZip-不支持PCL/UWP

System.IO.Compression-仅适用于流,无法压缩整个文件夹

我可以为每个平台分割实现(尽管它并不完美),但我仍然没有找到可以在UWP中使用的东西


在UWP库上工作时,您必须使用
System.IO.Compression
的流子系统,我们将向您提供任何帮助。当您需要.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;
    }

好的,我找到了这个名为SharpZipLib.Portable的项目,它也是一个开源项目 Github:


非常好:)

您能更详细地解释一下为什么不能使用IO.Compression吗?在ZipFile库中使用CreateFromDirectory我从来没有遇到过问题,但我也没有编写UWP或WP应用程序,所以也许这就是问题所在?哦,一定是错过了CreateFromDirectory:)我会尝试更简单,我只是在IO中没有ZipFile。压缩名称空间,我有:ZipArchive,GZipStream,DeflateStream-它们都只适用于流…我还不清楚,UWP/WP应用程序不能使用流?它们可以,但如何将流打开到文件夹?我尝试过使用类似的东西,但它没有按原样创建一个常规的gzip文件(例如,我可以在Linux服务器上使用uzip)。您是否成功创建了一个gzip格式的文件,该文件将被解压为一个文件夹,其中包含附加到流中的文件(clsEntry)?我能够将DotNetZip()重新编译为适用于Windows 8.1/Windows Phone 8.1的PCL,没有出现大问题,花了大约一个小时,但还没有尝试。我要做的大部分工作是替换文件系统调用。如果不执行文件读取和文件读取,则很难处理