How can i zip files in Java and not include files paths

Posted by Ignacio on Stack Overflow See other posts from Stack Overflow or by Ignacio
Published on 2010-06-10T21:15:35Z Indexed on 2010/06/10 21:42 UTC
Read the original article Hit count: 312

Filed under:
|
|

For example, i want to zip a file stored in /Users/me/Desktop/image.jpg

I maded this method:

public static Boolean generateZipFile(ArrayList<String> sourcesFilenames, String destinationDir, String zipFilename){
  // Create a buffer for reading the files 
  byte[] buf = new byte[1024]; 

  try {
   // VER SI HAY QUE CREAR EL ROOT PATH
         boolean result = (new File(destinationDir)).mkdirs();

         String zipFullFilename = destinationDir + "/" + zipFilename ;

         System.out.println(result);

   // Create the ZIP file  
   ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFullFilename)); 
   // Compress the files 
   for (String filename: sourcesFilenames) { 
    FileInputStream in = new FileInputStream(filename); 
    // Add ZIP entry to output stream. 
    out.putNextEntry(new ZipEntry(filename)); 
    // 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();

   return true;
  } catch (IOException e) { 
   return false;
  }  
 }

But when i extract the file, the unzipped files have the full path.

I don't want the full path of each file in the zip i only want the filename.

How can i made this?

© Stack Overflow or respective owner

Related posts about java

Related posts about zip