2010-03-31 20 views
7
में

मैं इस प्रकार एक ज़िप फ़ाइल बनाया था (एक साथ निर्देशिका के साथ) Windows के तहत:Windows में ज़िप फ़ाइल बनाएँ और ज़िप फ़ाइल निकालें लिनक्स

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(); 
     } 

    } 

} 

नव निर्मित ज़िप फ़ाइल कर सकते हैं http://www.exampledepot.com/egs/java.util.zip/GetZip.html

का उपयोग करके विंडोज़ के तहत समस्या के बिना निकाले जा सकते हैं, हालांकि, मुझे एहसास है कि क्या मैं http://www.exampledepot.com/egs/java.util.zip/GetZip.html के संशोधित संस्करण का उपयोग करके लिनक्स के तहत बनाई गई नई बनाई गई ज़िप फ़ाइल निकालता हूं। मूल संस्करण 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; 
} 

"MyDirectory \ MyFile.txt" के बदले MyFile.txt फ़ोल्डर MyDirectory के नीचे रखा जा रहा है।

मैं

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

में ज़िप फ़ाइल सृजन कोड को बदलने के द्वारा समस्या को हल करने की कोशिश लेकिन, यह एक पात्र समाधान है, द्वारा विभाजक हार्ड-कोडेड? क्या यह मैक ओएस के तहत काम करेगा? (मेरे पास कोशिश करने के लिए मैक नहीं है)

उत्तर

1

हां, आपका समाधान (हालांकि स्पष्ट रूप से सुरुचिपूर्ण) सही तरीका है। ज़िप्पेन्ट्री के अंदर "/" का उपयोग किया जाना चाहिए, स्थानीय फ़ाइल नहीं। सेपरेटर

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