SharpZipLib.Portable को लागू करने का प्रयास करते समय मैं यहां समाप्त हुआ। मैं इसे IVirtualFileSystem
के बिना उपयोग करना शुरू करता हूं क्योंकि मुझे पहले से ही एक लाइब्रेरी (PCLStorage) कहा जाता है जो जानता है कि फाइल सिस्टम में कैसे पढ़ना और लिखना है (इसे iOS
और Android
पर परीक्षण किया गया है)।
नोट: इस कार्यान्वयन एक पीसीएल को लक्षित iOS
, Android
के अंदर सभी कर रहे हैं। एंड्रॉइड या आईओएस के लिए कोई विशिष्ट कोड की आवश्यकता नहीं है।
यहाँ एक सरल उदाहरण है PCLStorage
और 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);
}
}
}
}
}
आप कैसे उपयोग करने के लिए SharpZipLib.Portable
आप यहां पढ़ सकते हैं (मूल SharpZipLib) पर कुछ उदाहरण प्राप्त करना चाहते हैं: Code reference और Zip samples।
वैकल्पिक:
कर मैं क्या समझाया ऊपर मैं क्योंकि मैं केवल ज़िप फ़ाइलों का समर्थन करने के लिए आवश्यक एक बहुत सरल समाधान के साथ समाप्त हो गया के बाद। मैंने ZipArchive Class
System.IO.Compression
और PCLStorage
में मौजूद, इसलिए इस समाधान के साथ मैंने 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);
}
}
}
}
}
:
यहाँ संस्करण है