2009-04-11 11 views
22

Diciamo che ho un file t.txt, una directory t e un altro file t/t2.txt. Se io uso l'utility linux zip "zip -r t.zip t.txt t", ottengo un file zip con le seguenti voci in loro (unzip -l t.zip):directory in un file zip quando si utilizza java.util.zip.ZipOutputStream

Archive: t.zip 
    Length  Date Time Name 
--------  ----  ----  ---- 
     9 04-11-09 09:11 t.txt 
     0 04-11-09 09:12 t/ 
     15 04-11-09 09:12 t/t2.txt 
--------       ------- 
     24       3 files 

Se provo per replicare tale comportamento con java.util.zip.ZipOutputStream e creare una voce zip per la directory, java genera un'eccezione. Può gestire solo i file. Posso creare una voce t/t2.txt nel file zip e aggiungere usare il contenuto del file t2.txt, ma non riesco a creare la directory. Perché?

risposta

30

ZipOutputStreampuò gestire le directory vuote con l'aggiunta di un forward-slash / dopo il nome della cartella. Prova (from)

public class Test { 
    public static void main(String[] args) { 
     try { 
      FileOutputStream f = new FileOutputStream("test.zip"); 
      ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(f)); 
      zip.putNextEntry(new ZipEntry("xml/")); 
      zip.putNextEntry(new ZipEntry("xml/xml")); 
      zip.close(); 
     } catch(Exception e) { 
      System.out.println(e.getMessage()); 
     } 
    } 
} 
+3

Ti sbagli John. I file zip gestiscono le directory vuote bene. Il trucco è che devi mettere una barra alla fine del nome. –

6

è possibile aggiungere "/" alla fine del nome della cartella. Basta usare il seguente comando:

zip.putNextEntry(new ZipEntry("xml/")); 
12

programma Java per Zip (cartelle contiene vuoti o pieni)

public class ZipUsingJavaUtil { 
    /* 
    * Zip function zip all files and folders 
    */ 
    @Override 
    @SuppressWarnings("finally") 
    public boolean zipFiles(String srcFolder, String destZipFile) { 
     boolean result = false; 
     try { 
      System.out.println("Program Start zipping the given files"); 
      /* 
      * send to the zip procedure 
      */ 
      zipFolder(srcFolder, destZipFile); 
      result = true; 
      System.out.println("Given files are successfully zipped"); 
     } catch (Exception e) { 
      System.out.println("Some Errors happned during the zip process"); 
     } finally { 
      return result; 
     } 
    } 

    /* 
    * zip the folders 
    */ 
    private void zipFolder(String srcFolder, String destZipFile) throws Exception { 
     ZipOutputStream zip = null; 
     FileOutputStream fileWriter = null; 
     /* 
     * create the output stream to zip file result 
     */ 
     fileWriter = new FileOutputStream(destZipFile); 
     zip = new ZipOutputStream(fileWriter); 
     /* 
     * add the folder to the zip 
     */ 
     addFolderToZip("", srcFolder, zip); 
     /* 
     * close the zip objects 
     */ 
     zip.flush(); 
     zip.close(); 
    } 

    /* 
    * recursively add files to the zip files 
    */ 
    private void addFileToZip(String path, String srcFile, ZipOutputStream zip, boolean flag) throws Exception { 
     /* 
     * create the file object for inputs 
     */ 
     File folder = new File(srcFile); 

     /* 
     * if the folder is empty add empty folder to the Zip file 
     */ 
     if (flag == true) { 
      zip.putNextEntry(new ZipEntry(path + "/" + folder.getName() + "/")); 
     } else { /* 
       * if the current name is directory, recursively traverse it 
       * to get the files 
       */ 
      if (folder.isDirectory()) { 
       /* 
       * if folder is not empty 
       */ 
       addFolderToZip(path, srcFile, zip); 
      } else { 
       /* 
       * write the file to the output 
       */ 
       byte[] buf = new byte[1024]; 
       int len; 
       FileInputStream in = new FileInputStream(srcFile); 
       zip.putNextEntry(new ZipEntry(path + "/" + folder.getName())); 
       while ((len = in.read(buf)) > 0) { 
        /* 
        * Write the Result 
        */ 
        zip.write(buf, 0, len); 
       } 
      } 
     } 
    } 

    /* 
    * add folder to the zip file 
    */ 
    private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) throws Exception { 
     File folder = new File(srcFolder); 

     /* 
     * check the empty folder 
     */ 
     if (folder.list().length == 0) { 
      System.out.println(folder.getName()); 
      addFileToZip(path, srcFolder, zip, true); 
     } else { 
      /* 
      * list the files in the folder 
      */ 
      for (String fileName : folder.list()) { 
       if (path.equals("")) { 
        addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, false); 
       } else { 
        addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip, false); 
       } 
      } 
     } 
    } 
} 
+3

Dovresti chiudere * FileInputStream *. – CSchulz

+1

In totale, è necessario chiudere FileInputStream. Comunque, è così bello! – Phuong

+0

Un improvviso dell'API: avere un file zipfiles ** (final String [] srcFolders, final String destZipFile) '** per aggiungere più cartelle al file zip. Semplicemente aggiungendo un ciclo for nel metodo 'zipFolder'. Le cartelle singole e multiple sono davvero facili da aggiungere in un file zip con questi metodi. – Velth

9

Come altri dire qui per aggiungere directory vuota aggiungere "/" al nome della directory. Prestare attenzione a NON aggiungere File.separator (uguale a "\") che in realtà aggiunge un file vuoto allo zip.

Mi ci è voluto un po 'per capire qual è stato il mio errore - spero di risparmiare altro tempo ...

Problemi correlati