2010-03-31 5 views
7

j'avais créé un fichier zip (avec répertoire) sous Windows comme suit (le code sont pris de http://www.exampledepot.com/egs/java.util.zip/CreateZip.html):Créer un fichier Zip dans Windows et extraction de fichiers Zip Dans Linux

package sandbox; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.util.zip.ZipEntry; 
import java.util.zip.ZipOutputStream; 

/** 
* 
* @author yan-cheng.cheok 
*/ 
public class Main { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // These are the files to include in the ZIP file 
     String[] filenames = new String[]{"MyDirectory" + File.separator + "MyFile.txt"}; 

     // Create a buffer for reading the files 
     byte[] buf = new byte[1024]; 

     try { 
      // Create the ZIP file 
      String outFilename = "outfile.zip"; 
      ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename)); 

      // Compress the files 
      for (int i=0; i<filenames.length; i++) { 
       FileInputStream in = new FileInputStream(filenames[i]); 

       // Add ZIP entry to output stream. 
       out.putNextEntry(new ZipEntry(filenames[i])); 

       // Transfer bytes from the file to the ZIP file 
       int len; 
       while ((len = in.read(buf)) > 0) { 
        out.write(buf, 0, len); 
       } 

       // Complete the entry 
       out.closeEntry(); 
       in.close(); 
      } 

      // Complete the ZIP file 
      out.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 

} 

Le fichier zip nouvellement créé peut être extrait sans problème sous Windows, en utilisant http://www.exampledepot.com/egs/java.util.zip/GetZip.html

Cependant, je réalise que si j'extrais le fichier zip nouvellement créé sous Linux, en utilisant la version modifiée de http://www.exampledepot.com/egs/java.util.zip/GetZip.html. La version d'origine ne vérifie pas le répertoire à l'aide de zipEntry.isDirectory()).

public static boolean extractZipFile(File zipFilePath, boolean overwrite) { 
    InputStream inputStream = null; 
    ZipInputStream zipInputStream = null; 
    boolean status = true; 

    try { 
     inputStream = new FileInputStream(zipFilePath); 

     zipInputStream = new ZipInputStream(inputStream); 
     final byte[] data = new byte[1024]; 

     while (true) { 
      ZipEntry zipEntry = null; 
      FileOutputStream outputStream = null; 

      try { 
       zipEntry = zipInputStream.getNextEntry(); 

       if (zipEntry == null) break; 

       final String destination = Utils.getUserDataDirectory() + zipEntry.getName(); 

       if (overwrite == false) { 
        if (Utils.isFileOrDirectoryExist(destination)) continue; 
       } 

       if (zipEntry.isDirectory()) 
       { 
        Utils.createCompleteDirectoryHierarchyIfDoesNotExist(destination); 
       } 
       else 
       { 
        final File file = new File(destination); 
        // Ensure directory is there before we write the file. 
        Utils.createCompleteDirectoryHierarchyIfDoesNotExist(file.getParentFile()); 

        int size = zipInputStream.read(data); 

        if (size > 0) { 
         outputStream = new FileOutputStream(destination); 

         do { 
          outputStream.write(data, 0, size); 
          size = zipInputStream.read(data); 
         } while(size >= 0); 
        } 
       } 
      } 
      catch (IOException exp) { 
       log.error(null, exp); 
       status = false; 
       break; 
      } 
      finally { 
       if (outputStream != null) { 
        try { 
         outputStream.close(); 
        } 
        catch (IOException exp) { 
         log.error(null, exp); 
         break; 
        } 
       } 

       if (zipInputStream != null) { 
        try { 
         zipInputStream.closeEntry(); 
        } 
        catch (IOException exp) { 
         log.error(null, exp); 
         break; 
        } 
       } 
      } 

     } // while(true) 
    } 
    catch (IOException exp) { 
     log.error(null, exp); 
     status = false; 
    } 
    finally { 
     if (zipInputStream != null) { 
      try { 
       zipInputStream.close(); 
      } catch (IOException ex) { 
       log.error(null, ex); 
      } 
     } 

     if (inputStream != null) { 
      try { 
       inputStream.close(); 
      } catch (IOException ex) { 
       log.error(null, ex); 
      } 
     } 
    } 
    return status; 
} 

"MonRépertoire \ MyFile.txt" au lieu de MyFile.txt être placé sous dossier MonRépertoire.

J'essaie de résoudre le problème en modifiant le code de création de fichier zip

String[] filenames = new String[]{"MyDirectory" + "/" + "MyFile.txt"}; 

Mais, est-ce une solution admissible, par Codés en dur le seperator? Cela fonctionnera-t-il sous Mac OS? (Je n'ai pas de Mac à essayer)

Répondre

1

Oui, votre solution (bien qu'apparemment inélégante) est la bonne façon. Le "/" doit être utilisé à l'intérieur de zipentry, pas le File.separator local

Questions connexes