如何实现SharpZipLib.Portable所需的VirtualFileSystem?

Mat*_*att 11 c# sharpziplib portable-class-library xamarin.forms

我想将SharpZipLib.Portable库添加到我的Xamarin.FormsPCL项目中.我的目标是Android和iOS.该文档提到您必须实现一个VirtualFileSystem才能使用该库,但我不知道该怎么做,我无法找到有关该主题的更多信息.

有没有人使用这个库可以指导我使用它所需的步骤?

jze*_*ino 12

我在尝试实现SharpZipLib.Portable时最终到了这里.我开始使用它没有IVirtualFileSystem因为我已经有一个名为(PCLStorage)的库知道如何在文件系统中读取和写入(测试它iOSAndroid).

注意:此实现都在PCL定位iOSAndroid.不需要Android或iOS的特定代码.

这是一个如何使用PCLStorage和提取Zip文件的简单示例SharpZipLib.Portable:

public async void DonwLoadAndStoreZipFile()
{
    var bytes = await DownloadImageAsync("https://github.com/fluidicon.png");

    // IFolder interface comes from PCLStorage    
    IFolder rootFolder = FileSystem.Current.LocalStorage;
    IFolder folder = await rootFolder.CreateFolderAsync("zipFolder", CreationCollisionOption.OpenIfExists);
    IFile file = await folder.CreateFileAsync("test.zip" , CreationCollisionOption.OpenIfExists);

    using (Stream stream = await file.OpenAsync(FileAccess.ReadAndWrite))
    {
        await stream.WriteAsync(bytes, 0, bytes.Length);
        using (var zf = new ZipFile(stream))
        {
            foreach (ZipEntry zipEntry in zf) 
            {                
                // Gete Entry Stream.
                Stream zipEntryStream = zf.GetInputStream(zipEntry);

                // Create the file in filesystem and copy entry stream to it.
                IFile zipEntryFile = await rootFolder.CreateFileAsync(zipEntry.Name , CreationCollisionOption.FailIfExists);
                using(Stream outPutFileStream = await zipEntryFile.OpenAsync(FileAccess.ReadAndWrite))
                {
                    await zipEntryStream.CopyToAsync(outPutFileStream);
                }
            }
        }
    }                    
}
Run Code Online (Sandbox Code Playgroud)

如果你想获得一些如何使用的例子,SharpZipLib.Portable你可以在这里阅读(原始SharpZipLib): 代码参考Zip样本.

替代方案:

在做了我上面解释的之后,我得到了一个更简单的解决方案,因为我只需要支持ZIP文件.我曾经ZipArchive Class存在于System.IO.CompressionPCLStorage,所以这种解决方案我不使用SharpZipLib.Portable.

这是版本:

public async void DonwLoadAndStoreZipFile()
{
    var bytes = await DownloadImageAsync(https://github.com/fluidicon.png);

    // IFolder interface comes from PCLStorage
    IFolder rootFolder = FileSystem.Current.LocalStorage;
    IFolder folder = await rootFolder.CreateFolderAsync("zipFolder", CreationCollisionOption.OpenIfExists);
    IFile file = await folder.CreateFileAsync("test.zip" , CreationCollisionOption.OpenIfExists);

    using (Stream stream = await file.OpenAsync(FileAccess.ReadAndWrite))
    {
        await stream.WriteAsync(bytes, 0, bytes.Length);
        using(ZipArchive archive = new ZipArchive(stream))
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                IFile zipEntryFile = await rootFolder.CreateFileAsync(entry.Name, CreationCollisionOption.FailIfExists);
                using (Stream outPutStream = await zipEntryFile.OpenAsync(FileAccess.ReadAndWrite))
                {
                    await entry.Open().CopyToAsync(outPutStream);
                }
            }
        }
    }                    
}
Run Code Online (Sandbox Code Playgroud)