Ich landete hier beim Versuch, die SharpZipLib.Portable zu implementieren. Ich benutze es ohne die IVirtualFileSystem
, weil ich bereits eine Bibliothek namens (PCLStorage), die lesen und schreiben im Dateisystem (getestet auf iOS
und Android
) hatte.
HINWEIS: Diese Implementierungen sind alle in einem PCL-Targeting iOS
, Android
. Kein spezifischer Code für Android oder iOS wird benötigt.
Hier ist ein einfaches Beispiel, wie man eine Zip-Datei PCLStorage
und SharpZipLib.Portable
mit extrahieren:
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);
}
}
}
}
}
Wenn Sie einige Beispiele zu bekommen, wie die SharpZipLib.Portable
Sie hier lesen (original SharpZipLib) können verwendet werden: Code reference und Zip samples.
ALTERNATIVE:
Nach tun, was ich oben erläuterte ich mit einer viel einfacheren Lösung endete, weil ich nur ZIP-Dateien zur Unterstützung benötigt. Ich verwendete ZipArchive Class
in System.IO.Compression
und PCLStorage
, so dass ich mit dieser Lösung nicht verwendet SharpZipLib.Portable
. Hier
ist die Version:
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);
}
}
}
}
}