2009-03-25 22 views
23

एससीओआरएम पैकेज बनाने के लिए गतिशील रूप से कुछ फ़ाइलों को .zip में पैकेज करने की आवश्यकता है, किसी को पता है कि कोड का उपयोग करके यह कैसे किया जा सकता है? क्या .zip के अंदर गतिशील रूप से फ़ोल्डर संरचना को भी बनाना संभव है?ASP.NET का उपयोग करके ज़िप फ़ाइल कैसे बनाएं और भरें?

+0

[ज़िप फ़ाइल जेनरेट करने के लिए System.IO.Packaging का उपयोग करना] का संभावित डुप्लिकेट (http://stackoverflow.com/questions/6386113/using-system-io-packaging-to-generate-a-zip-file) – Will

उत्तर

14

अब आपको बाहरी पुस्तकालय का उपयोग करने की आवश्यकता नहीं है। System.IO.Packaging में कक्षाएं हैं जिनका उपयोग सामग्री को ज़िप फ़ाइल में छोड़ने के लिए किया जा सकता है। हालांकि, यह आसान नहीं है। Here's a blog post with an example (इसके अंत में; इसके लिए खुदाई)।


लिंक स्थिर नहीं है, इसलिए यहां पोस्ट जॉन प्रदान किया गया उदाहरण है।

using System; 
using System.IO; 
using System.IO.Packaging; 

namespace ZipSample 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      AddFileToZip("Output.zip", @"C:\Windows\Notepad.exe"); 
      AddFileToZip("Output.zip", @"C:\Windows\System32\Calc.exe"); 
     } 

     private const long BUFFER_SIZE = 4096; 

     private static void AddFileToZip(string zipFilename, string fileToAdd) 
     { 
      using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate)) 
      { 
       string destFilename = ".\\" + Path.GetFileName(fileToAdd); 
       Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative)); 
       if (zip.PartExists(uri)) 
       { 
        zip.DeletePart(uri); 
       } 
       PackagePart part = zip.CreatePart(uri, "",CompressionOption.Normal); 
       using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read)) 
       { 
        using (Stream dest = part.GetStream()) 
        { 
         CopyStream(fileStream, dest); 
        } 
       } 
      } 
     } 

     private static void CopyStream(System.IO.FileStream inputStream, System.IO.Stream outputStream) 
     { 
      long bufferSize = inputStream.Length < BUFFER_SIZE ? inputStream.Length : BUFFER_SIZE; 
      byte[] buffer = new byte[bufferSize]; 
      int bytesRead = 0; 
      long bytesWritten = 0; 
      while ((bytesRead = inputStream.Read(buffer, 0, buffer.Length)) != 0) 
      { 
       outputStream.Write(buffer, 0, bytesRead); 
       bytesWritten += bytesRead; 
      } 
     } 
    } 
} 
+0

+1 अत्यंत उपयोगी लिंक है कि लेखक तारीख तक रखने के लिए है। –

+0

भी इस लिंक http://weblogs.asp.net/dneimke/archive/2005/02/25/380273.aspx – daitangio

+0

लिंक टूट गया है – bpoiss

6

आप SharpZipLib पर एक नज़र डाल सकते हैं। और यहां एक sample है।

+0

मुझे 15 सेकंड तक हराया ... – JoshBerke

1

मैंने इस के लिए चिल्काट से एक मुफ्त घटक का उपयोग किया है: http://www.chilkatsoft.com/zip-dotnet.asp। क्या मुझे बहुत कुछ चाहिए जो मुझे चाहिए, हालांकि मुझे फ़ाइल संरचना को गतिशील रूप से बनाने के बारे में निश्चित नहीं है।

+0

हमने इस कंपनी का भी हमारी कंपनी में उपयोग किया। घटक में त्रुटियों के आधार पर हमारी कुछ अत्यधिक तनावग्रस्त सेवाओं ने इंजन अपवाद का कारण बना दिया। माइक्रोसॉफ्ट सपोर्ट टिकट के बाद हमने SharpZLib पर स्विच करने का फैसला किया। यह 2 साल पहले था। मुझे नहीं पता कि आज घटक कितना अच्छा है? –

+0

हमें इसके साथ कभी भी कोई समस्या नहीं हुई है, हालांकि निर्यात सेवाओं में उपयोग किया जाता है जो आमतौर पर हर घंटे चलते हैं, लेकिन आम तौर पर दैनिक।एन्क्रिप्शन उपयोगी साथ ही – Macros

20

DotNetZip इसके लिए अच्छा है। Working example

आप ज़िप को सीधे Response.OutputStream पर लिख सकते हैं। कोड इस तरह दिखता है:

Response.Clear(); 
    Response.BufferOutput = false; // for large files... 
    System.Web.HttpContext c= System.Web.HttpContext.Current; 
    String ReadmeText= "Hello!\n\nThis is a README..." + DateTime.Now.ToString("G"); 
    string archiveName= String.Format("archive-{0}.zip", 
             DateTime.Now.ToString("yyyy-MMM-dd-HHmmss")); 
    Response.ContentType = "application/zip"; 
    Response.AddHeader("content-disposition", "filename=" + archiveName); 

    using (ZipFile zip = new ZipFile()) 
    { 
     // filesToInclude is an IEnumerable<String>, like String[] or List<String> 
     zip.AddFiles(filesToInclude, "files");    

     // Add a file from a string 
     zip.AddEntry("Readme.txt", "", ReadmeText); 
     zip.Save(Response.OutputStream); 
    } 
    // Response.End(); // no! See http://stackoverflow.com/questions/1087777 
    Response.Close(); 

डॉटनेटजिप निःशुल्क है।

0

ज़िप पर "फ्लाई पर" ज़िप फ़ाइल बनाना हमारे Rebex ZIP घटक का उपयोग करके किया जाएगा।

निम्न नमूना यह पूरी तरह से वर्णन करता है, एक सबफ़ोल्डर बनाने सहित:

// prepare MemoryStream to create ZIP archive within 
using (MemoryStream ms = new MemoryStream()) 
{ 
    // create new ZIP archive within prepared MemoryStream 
    using (ZipArchive zip = new ZipArchive(ms)) 
    {    
     // add some files to ZIP archive 
     zip.Add(@"c:\temp\testfile.txt"); 
     zip.Add(@"c:\temp\innerfile.txt", @"\subfolder"); 

     // clear response stream and set the response header and content type 
     Response.Clear(); 
     Response.ContentType = "application/zip"; 
     Response.AddHeader("content-disposition", "filename=sample.zip"); 

     // write content of the MemoryStream (created ZIP archive) to the response stream 
     ms.WriteTo(Response.OutputStream); 
    } 
} 

// close the current HTTP response and stop executing this page 
HttpContext.Current.ApplicationInstance.CompleteRequest(); 
0

आप .NET फ्रेमवर्क 4.5 या नए आप उपयोग कर रहे हैं तीसरे पक्ष के पुस्तकालयों से बच सकते हैं और System.IO.Compression.ZipArchive मूल वर्ग का उपयोग कर सकते हैं।

यहां एक त्वरित कोड एक MemoryStream और दो फ़ाइलों का प्रतिनिधित्व बाइट सरणियों की एक जोड़ी का उपयोग कर नमूना है:

byte[] file1 = GetFile1ByteArray(); 
byte[] file2 = GetFile2ByteArray(); 

using (MemoryStream ms = new MemoryStream()) 
{ 
    using (var archive = new ZipArchive(ms, ZipArchiveMode.Create, true)) 
    { 
     var zipArchiveEntry = archive.CreateEntry("file1.txt", CompressionLevel.Fastest); 
     using (var zipStream = zipArchiveEntry.Open()) zipStream.Write(file1, 0, file1.Length); 
     zipArchiveEntry = archive.CreateEntry("file2.txt", CompressionLevel.Fastest); 
     using (var zipStream = zipArchiveEntry.Open()) zipStream.Write(file2, 0, file2.Length); 
    } 
    return File(ms.ToArray(), "application/zip", "Archive.zip"); 
} 

आप लौटने एक MVC नियंत्रक के अंदर उपयोग कर सकते हैं एक ActionResult: वैकल्पिक रूप से, आपके द्वारा बनाए गए phisically करने की आवश्यकता है ज़िप संग्रह, आप या तो MemoryStream डिस्क पर या FileStream के साथ पूरी तरह से बदल सकते हैं।

इस विषय के बारे में अधिक जानकारी के लिए आप read this post भी कर सकते हैं।

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