2015-07-06 33 views
8

मैं SharpZipLib.Portable लाइब्रेरी को अपने Xamarin.Forms पीसीएल प्रोजेक्ट में जोड़ना चाहता हूं। मैं एंड्रॉइड और आईओएस को लक्षित कर रहा हूं। प्रलेखन का उल्लेख है कि आपको पुस्तकालय का उपयोग करने के लिए VirtualFileSystem लागू करना होगा, लेकिन मुझे नहीं पता कि यह कैसे करें और मैं इस विषय पर अधिक जानकारी प्राप्त करने में असमर्थ हूं।मैं SharpZipLib.Portable द्वारा आवश्यक वर्चुअलफाइल सिस्टम को कैसे कार्यान्वित करूं?

क्या कोई इस पुस्तकालय का उपयोग करता है जो इसे उपयोग करने के लिए आवश्यक चरणों में मार्गदर्शन कर सकता है?

उत्तर

10

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 ClassSystem.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); 
       } 
      } 
     } 
    }      
} 
:

यहाँ संस्करण है

संबंधित मुद्दे