2010-10-18 3 views
7
public static void main(String argv[]) { 
    try { 
     String date = new java.text.SimpleDateFormat("MM-dd-yyyy") 
       .format(new java.util.Date()); 
     File inFolder = new File("Output/" + date + "_4D"); 
     File outFolder = new File("Output/" + date + "_4D" + ".zip"); 
     ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(
       new FileOutputStream(outFolder))); 
     BufferedInputStream in = null; 
     byte[] data = new byte[1000]; 
     String files[] = inFolder.list(); 
     for (int i = 0; i < files.length; i++) { 
      in = new BufferedInputStream(new FileInputStream(
        inFolder.getPath() + "/" + files[i]), 1000); 
      out.putNextEntry(new ZipEntry(files[i])); 
      int count; 
      while ((count = in.read(data, 0, 1000)) != -1) { 
       out.write(data, 0, count); 
      } 
      out.closeEntry(); 
     } 
     out.flush(); 
     out.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

मैं सबफ़ोल्डर वाले फ़ोल्डर को ज़िप करने का प्रयास कर रहा हूं। 10-18-2010_4 डी नामक फ़ोल्डर को ज़िप करने का प्रयास कर रहा है। उपरोक्त प्रोग्राम निम्न अपवाद के साथ समाप्त होता है। कृपया इस मुद्दे को साफ़ करने के बारे में सलाह दें।एक फ़ोल्डर को ज़िप करना जिसमें उपफोल्डर्स

java.io.FileNotFoundException: Output\10-18-2010_4D\4D (Access is denied) 
    at java.io.FileInputStream.open(Native Method) 
    at java.io.FileInputStream.<init>(Unknown Source) 
    at java.io.FileInputStream.<init>(Unknown Source) 
    at ZipFile.main(ZipFile.java:17) 
+0

अपवाद में फ़ोल्डर का नाम और जो आपने उल्लेख किया है वह अलग है। – ivorykoder

+0

java.util.zip.ZipOutputStream का उपयोग करते समय [ज़िप फ़ाइल में निर्देशिकाओं के संभावित डुप्लिकेट] (http://stackoverflow.com/questions/740375/directories-in-a-zip-file-when-using-java-util -zip-zipoutputstream) –

उत्तर

5

यदि फ़ाइल एक निर्देशिका है जांच करने की जरूरत है क्योंकि आप ज़िप विधि के लिए निर्देशिका पारित नहीं कर सकते हैं।

this page पर एक नज़र डालें जो दिखाता है कि आप किसी दिए गए निर्देशिका को दोबारा कैसे जोड़ सकते हैं।

+0

मुझे लगता है कि @ डोगबेन सही है। मैंने केवल एक फाइल वाली निर्देशिका का उपयोग करके अपना कोड चलाया, और यह इरादे के रूप में काम करता था। जैसे ही मैंने नेस्टेड डायरेक्टर को जोड़ा, मुझे एफएनएफ (एक्सेस अस्वीकार कर दिया गया) अपवाद मिला। –

2

मैं ant task for zipping शामिल करना चाहता हूं - यह काम करना आसान तरीका है।

काम वर्ग यहां पाया जा सकता: org.apache.tools.ant.taskdefs.Zip (यह प्रोग्राम के उपयोग करें)

+0

क्या आप कुछ बिंदु दे सकते हैं कि हमें ज़िप के लिए चींटी कार्य क्यों करना चाहिए? – ivorykoder

+1

यह कोड की 3 लाइनों में किया गया है, और यह काम करता है। उपरोक्त की तुलना करें। – Bozho

+0

@ बोझो मैं चींटी के हाल के संस्करण में कई जार फ़ाइलों को पा सकता हूं। ज़िप फ़ोल्डरों के लिए किस का उपयोग किया जाना चाहिए? – LGAP

0
private void zipFiles (ArrayList listWithFiles, String zipName) { 
    try { 

     byte[] buffer = new byte[1024]; 

     // create object of FileOutputStream 
     FileOutputStream fout = new FileOutputStream(zipName); 

     // create object of ZipOutputStream from FileOutputStream 
     ZipOutputStream zout = new ZipOutputStream(fout); 

     for (String currentFile : listWithFiles) { 

      // create object of FileInputStream for source file 
      FileInputStream fin = new FileInputStream(currentFile); 

      // add files to ZIP 
      zout.putNextEntry(new ZipEntry(currentFile)); 

      // write file content 
      int length; 

      while ((length = fin.read(buffer)) > 0) { 
       zout.write(buffer, 0, length); 
      } 

      zout.closeEntry(); 

      // close the InputStream 
      fin.close(); 
     } 

     // close the ZipOutputStream 
     zout.close(); 
    } catch (IOException ioe) { 
     System.out.println("IOException :" + ioe); 
    } 
} 
+1

यह उपफोल्डर के मुद्दे को हल नहीं करता है – Comencau

18

ज़िप संग्रह बनाने के लिए यहां कोड है। बनाया गया संग्रह मूल निर्देशिका संरचना (यदि कोई हो) को संरक्षित करता है।

public static void addDirToZipArchive(ZipOutputStream zos, File fileToZip, String parrentDirectoryName) throws Exception { 
    if (fileToZip == null || !fileToZip.exists()) { 
     return; 
    } 

    String zipEntryName = fileToZip.getName(); 
    if (parrentDirectoryName!=null && !parrentDirectoryName.isEmpty()) { 
     zipEntryName = parrentDirectoryName + "/" + fileToZip.getName(); 
    } 

    if (fileToZip.isDirectory()) { 
     System.out.println("+" + zipEntryName); 
     for (File file : fileToZip.listFiles()) { 
      addDirToZipArchive(zos, file, zipEntryName); 
     } 
    } else { 
     System.out.println(" " + zipEntryName); 
     byte[] buffer = new byte[1024]; 
     FileInputStream fis = new FileInputStream(fileToZip); 
     zos.putNextEntry(new ZipEntry(zipEntryName)); 
     int length; 
     while ((length = fis.read(buffer)) > 0) { 
      zos.write(buffer, 0, length); 
     } 
     zos.closeEntry(); 
     fis.close(); 
    } 
} 

इस विधि को कॉल करने के बाद आउटपुट स्ट्रीम को बंद करना न भूलें। यहां उदाहरण दिया गया है:

public static void main(String[] args) throws Exception { 
    FileOutputStream fos = new FileOutputStream("C:\\Users\\vebrpav\\archive.zip"); 
    ZipOutputStream zos = new ZipOutputStream(fos); 
    addDirToZipArchive(zos, new File("C:\\Users\\vebrpav\\Downloads\\"), null); 
    zos.flush(); 
    fos.flush(); 
    zos.close(); 
    fos.close(); 
} 
0

यहां मैंने लिखा है। यह उदाहरण फ़ाइलों की संरचना को बनाए रखता है और इसके द्वारा, डुप्लिकेट प्रविष्टि अपवाद से बचें।

/** 
    * Compress a directory to ZIP file including subdirectories 
    * @param directoryToCompress directory to zip 
    * @param outputDirectory  where to place the compress file 
    */ 
    public void zipDirectory(File directoryToCompress, File outputDirectory){ 
     try { 
      FileOutputStream dest = new FileOutputStream(new File(outputDirectory, directoryToCompress.getName() + ".zip")); 
      ZipOutputStream zipOutputStream = new ZipOutputStream(dest); 

      zipDirectoryHelper(directoryToCompress, directoryToCompress, zipOutputStream); 
      zipOutputStream.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
    } 

    private void zipDirectoryHelper(File rootDirectory, File currentDirectory, ZipOutputStream out) throws Exception { 
     byte[] data = new byte[2048]; 

     File[] files = currentDirectory.listFiles(); 
     if (files == null) { 
      // no files were found or this is not a directory 

     } else { 
      for (File file : files) { 
       if (file.isDirectory()) { 
        zipDirectoryHelper(rootDirectory, file, out); 
       } else { 
        FileInputStream fi = new FileInputStream(file); 
        // creating structure and avoiding duplicate file names 
        String name = file.getAbsolutePath().replace(rootDirectory.getAbsolutePath(), ""); 

        ZipEntry entry = new ZipEntry(name); 
        out.putNextEntry(entry); 
        int count; 
        BufferedInputStream origin = new BufferedInputStream(fi,2048); 
        while ((count = origin.read(data, 0 , 2048)) != -1){ 
         out.write(data, 0, count); 
        } 
        origin.close(); 
       } 
      } 
     } 

    }