首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在不包含文件路径的情况下使用Java压缩文件

如何在不包含文件路径的情况下使用Java压缩文件
EN

Stack Overflow用户
提问于 2010-06-11 05:15:36
回答 7查看 22.7K关注 0票数 20

例如,我想压缩存储在/Users/me/Desktop/image.jpg中的文件

我做了这个方法:

代码语言:javascript
复制
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;
  }  
 }

但是当我解压缩文件时,解压缩后的文件具有完整的路径。

我不想要压缩包中每个文件的完整路径,我只需要文件名。

我该怎么做呢?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3018579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档