【问题标题】:How do I implement the VirtualFileSystem required by SharpZipLib.Portable?如何实现 SharpZipLib.Portable 所需的 VirtualFileSystem?
【发布时间】:2015-07-06 14:28:56
【问题描述】:

我想将SharpZipLib.Portable 库添加到我的Xamarin.Forms PCL 项目中。我的目标是 Android 和 iOS。文档提到您必须实现 VirtualFileSystem 才能使用该库,但我不知道该怎么做,而且我无法找到有关此主题的大量信息。

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

【问题讨论】:

    标签: c# xamarin.forms portable-class-library sharpziplib


    【解决方案1】:

    我在尝试实现SharpZipLib.Portable 时到了这里。 我开始在没有 IVirtualFileSystem 的情况下使用它,因为我已经有一个名为 (PCLStorage) 的库,它知道如何在文件系统中读写(在 iOSAndroid 上进行了测试)。

    注意:这些实现都在针对iOSAndroid 的PCL 中。无需针对 Android 或 iOS 的特定代码。

    这里是一个简单的例子,如何使用PCLStorageSharpZipLib.Portable 提取一个 Zip 文件:

    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);
                    }
                }
            }
        }                    
    }
    

    如果你想获得一些关于如何使用SharpZipLib.Portable 的例子,你可以在这里阅读(原文SharpZipLib): Code referenceZip samples.

    替代方案:

    完成我上面解释的操作后,我得到了一个更简单的解决方案,因为我只需要支持 ZIP 文件。 我在System.IO.CompressionPCLStorage 中使用了ZipArchive Class,所以在这个解决方案中我没有使用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);
                    }
                }
            }
        }                    
    }
    

    【讨论】:

      【解决方案2】:

      这对我有用:

      1. 仅为 PCL 项目安装 SharpZipLib.Portable

      2. 使用代码:

        using ICSharpCode.SharpZipLib.Core;
        using ICSharpCode.SharpZipLib.Zip;
        
        public void ExtractZipFile(string archivePath, string password, string outFolder) {
        
            using(Stream fsInput = File.OpenRead(archivePath)) 
            using(zf = new ZipFile(fs)){
        
            if (!String.IsNullOrEmpty(password)) {
                zf.Password = password;
            }
        
            foreach (ZipEntry zipEntry in zf) {
                if (!zipEntry.IsFile) {
                    continue;
                }
                String entryFileName = zipEntry.Name;
        
                var fullZipToPath = Path.Combine(outFolder, entryFileName);
                var directoryName = Path.GetDirectoryName(fullZipToPath);
                if (directoryName.Length > 0) {
                    Directory.CreateDirectory(directoryName);
                }
        
                var buffer = new byte[4096];
        
                using(var zipStream = zf.GetInputStream(zipEntry))
                using (Stream fsOutput = File.Create(fullZipToPath)) {
                    StreamUtils.Copy(zipStream, fsOutput , buffer);
                }
            }
        }
        }
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-16
        • 2016-04-02
        • 2018-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多