2008-11-30 11 views
29

मैं एक ज़िप संग्रह से निर्दिष्ट फ़ाइलों को खींचने के लिए SharpZipLib का उपयोग करने की कोशिश कर रहा हूं। उदाहरण मैं हमेशा देखा है के सभी उम्मीद करते हैं कि आप पूरे ज़िप अनज़िप करने के लिए चाहते हैं, और की तर्ज पर कुछ करना:विशिष्ट फ़ाइलों को अनजिप करने के लिए SharpZipLib का उपयोग करना?

ZipEntry entry = zipInStream.SeekToFile("FileName"); 

:

 FileStream fileStreamIn = new FileStream (sourcePath, FileMode.Open, FileAccess.Read); 

     ZipInputStream zipInStream = new ZipInputStream(fileStreamIn); 
     ZipEntry entry; 

     while (entry = zipInStream.GetNextEntry() != null) 
     { 
      // Unzip file 
     } 

मुझे क्या करना चाहते हैं कुछ की तरह है चूंकि मेरी ज़रूरतें एक ज़िप के रूप में एक ज़िप के रूप में उपयोग करने और आवश्यकतानुसार स्मृति में फ़ाइलों को पकड़ने में शामिल हैं।

क्या कोई SharpZipLib से परिचित है? क्या किसी को पता है कि मैं पूरे ज़िप से हाथ से चलने के बिना ऐसा कर सकता हूं?

उत्तर

41

ZipFile.GetEntry चाल करना चाहिए:

using (var fs = new FileStream(sourcePath, FileMode.Open, FileAccess.Read)) 
using (var zf = new ZipFile(fs)) { 
    var ze = zf.GetEntry(fileName); 
    if (ze == null) { 
     throw new ArgumentException(fileName, "not found in Zip"); 
    } 

    using (var s = zf.GetInputStream(ze)) { 
     // do something with ZipInputStream 
    } 
} 
+0

अगर मैं एक फ़ोल्डर में एक ज़िप फ़ाइल से कई फ़ाइलों को अलग करना चाहते हैं, उसको कैसे करे?उदाहरण के लिए, मैं उपसर्ग "अच्छा" के साथ फाइलें प्राप्त करना चाहता हूं, यह मेरे कोड के साथ कैसे करें: "var ze = zf.GetEntry (" good * ");"। किसी भी विचार के लिए धन्यवाद। –

13
FastZip.ExtractZip (string zipFileName, string targetDirectory, string fileFilter) 

एक फ़ाइल फिल्टर के आधार पर एक या अधिक फ़ाइलें निकाल सकते हैं (यानी नियमित expressoin स्ट्रिंग)

यहाँ फ़ाइल फिल्टर के बारे में डॉक्टर :

// A filter is a sequence of independant <see cref="Regex">regular expressions</see> separated by semi-colons ';' 
// Each expression can be prefixed by a plus '+' sign or a minus '-' sign to denote the expression 
// is intended to include or exclude names. If neither a plus or minus sign is found include is the default 
// A given name is tested for inclusion before checking exclusions. Only names matching an include spec 
// and not matching an exclude spec are deemed to match the filter. 
// An empty filter matches any name. 
// </summary> 
// <example>The following expression includes all name ending in '.dat' with the exception of 'dummy.dat' 
// "+\.dat$;-^dummy\.dat$" 
तो myfile.dat आप इस्तेमाल कर सकते हैं नाम की एक फ़ाइल के लिए

अपनी फ़ाइल fil के रूप में "+ * myFile \ .dat $।" टेर।

6

DotNetZip में यह वास्तव में आसान बनाने के लिए ज़िपफाइल कक्षा पर एक स्ट्रिंग इंडेक्सर है।

using (ZipFile zip = ZipFile.Read(sourcePath) 
{ 
    zip["NameOfFileToUnzip.txt"].Extract(); 
} 

आपको फ़ाइल को निकालने के लिए इनपुटस्ट्रीम और आउटपुटस्ट्रीम के साथ परेशान करने की आवश्यकता नहीं है। दूसरी ओर यदि आप स्ट्रीम चाहते हैं, तो आप इसे प्राप्त कर सकते हैं:

using (ZipFile zip = ZipFile.Read(sourcePath) 
{ 
    Stream s = zip["NameOfFileToUnzip.txt"].OpenReader(); 
    // fiddle with stream here 
} 

आप वाइल्डकार्ड निष्कर्ष भी कर सकते हैं।

using (ZipFile zip = ZipFile.Read(sourcePath) 
{ 
    // extract all XML files in the archive 
    zip.ExtractSelectedEntries("*.xml"); 
} 

निर्दिष्ट करने के लिए ऊपर लिख/no-ऊपर लिख भार के, अलग लक्ष्य निर्देशिका, आदि तुम भी फ़ाइल नाम के अलावा अन्य मानदंडों के आधार पर निकाल सकते हैं कर रहे हैं। उदाहरण के लिए, सभी फाइलों को नए निकालने 15 जनवरी, 2009 की तुलना में:

 // extract all files modified after 15 Jan 2009 
    zip.ExtractSelectedEntries("mtime > 2009-01-15"); 

और तुम मापदंड को जोड़ सकते हैं:

 // extract all files that are modified after 15 Jan 2009) AND larger than 1mb 
    zip.ExtractSelectedEntries("mtime > 2009-01-15 and size > 1mb"); 

    // extract all XML files that are modified after 15 Jan 2009) AND larger than 1mb 
    zip.ExtractSelectedEntries("name = *.xml and mtime > 2009-01-15 and size > 1mb"); 

आप फ़ाइलों आपके द्वारा चुने गए निकालने के लिए नहीं है। आप बस उन्हें चुन सकते हैं और फिर निकालने या नहीं करने के बारे में निर्णय ले सकते हैं।

using (ZipFile zip1 = ZipFile.Read(ZipFileName)) 
    { 
     var PhotoShopFiles = zip1.SelectEntries("*.psd"); 
     // the selection is just an ICollection<ZipEntry> 
     foreach (ZipEntry e in PhotoShopFiles) 
     { 
      // examine metadata here, make decision on extraction 
      e.Extract(); 
     } 
    } 
2

मुझे ज़िप संग्रहकर्ताओं से विशिष्ट फ़ाइलों को निकालने के लिए VB.NET का विकल्प मिलता है। स्पेनिश में टिप्पणियाँ। एक ज़िप फ़ाइल पथ लें, फ़ाइल नाम जिसे आप निकालना चाहते हैं, यदि आवश्यक हो तो पासवर्ड। मूल पथ पर निकालें यदि मूलपाथ 1 पर सेट किया गया है, या OriginalPath = 0 पर DestPath का उपयोग करें। यह इस प्रकार है के रूप में "ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream" सामान में नजर रखें ...

:

''' <summary> 
''' Extrae un archivo específico comprimido dentro de un archivo zip 
''' </summary> 
''' <param name="SourceZipPath"></param> 
''' <param name="FileName">Nombre del archivo buscado. Debe incluir ruta, si se comprimió usando guardar con FullPath</param> 
''' <param name="DestPath">Ruta de destino del archivo. Ver parámetro OriginalPath.</param> 
''' <param name="password">Si el archivador no tiene contraseña, puede quedar en blanco</param> 
''' <param name="OriginalPath">OriginalPath=1, extraer en la RUTA ORIGINAL. OriginalPath=0, extraer en DestPath</param> 
''' <returns></returns> 
''' <remarks></remarks> 
Public Function ExtractSpecificZipFile(ByVal SourceZipPath As String, ByVal FileName As String, _ 
ByVal DestPath As String, ByVal password As String, ByVal OriginalPath As Integer) As Boolean 
    Try 
     Dim fileStreamIn As FileStream = New FileStream(SourceZipPath, FileMode.Open, FileAccess.Read) 
     Dim fileStreamOut As FileStream 
     Dim zf As ZipFile = New ZipFile(fileStreamIn) 

     Dim Size As Integer 
     Dim buffer(4096) As Byte 

     zf.Password = password 

     Dim Zentry As ZipEntry = zf.GetEntry(FileName) 

     If (Zentry Is Nothing) Then 
      Debug.Print("not found in Zip") 
      Return False 
      Exit Function 
     End If 

     Dim fstr As ICSharpCode.SharpZipLib.Zip.Compression.Streams.InflaterInputStream 
     fstr = zf.GetInputStream(Zentry) 

     If OriginalPath = 1 Then 
      Dim strFullPath As String = Path.GetDirectoryName(Zentry.Name) 
      Directory.CreateDirectory(strFullPath) 
      fileStreamOut = New FileStream(strFullPath & "\" & Path.GetFileName(Zentry.Name), FileMode.Create, FileAccess.Write) 
     Else 
      fileStreamOut = New FileStream(DestPath + "\" + Path.GetFileName(Zentry.Name), FileMode.Create, FileAccess.Write) 
     End If 


     Do 
      Size = fstr.Read(buffer, 0, buffer.Length) 
      fileStreamOut.Write(buffer, 0, Size) 
     Loop While (Size > 0) 

     fstr.Close() 
     fileStreamOut.Close() 
     fileStreamIn.Close() 
     Return True 
    Catch ex As Exception 
     Return False 
    End Try 

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