2016-04-26 61 views
0

我想压缩包含文件和子目录的目录。我做到了这一点,工作得很好,但我越来越不寻常和好奇的文件结构(至少我看到了这种方式)。使用Java压缩目录时获取奇怪的结构文件

这是创建的文件:Empty zip file当我点击它,我看到这样一个“空”目录:enter image description here但是当我解压这个我看到这个文件结构(不是所有的名称是exacly,因为它们是在显示下图):

|mantenimiento 
    |Carpeta_A 
    |File1.txt 
    |File2.txt 
    |Carpeta_B 
    |Sub_carpetaB 
     |SubfileB.txt 
    |Subfile1B.txt 
    |Subfile2B.txt 
    |File12.txt 

我的问题在某种程度上是该文件夹“mantenimiento”就是我从(我要压缩的目录)zippping,我不希望它在那里,所以当我解压刚刚创建的.zip文件我想要它与这个文件结构(这是“mantenimiento”目录内的文件和目录):Correct directory structure另一件事是当我点击.zip文件我想看到文件nd目录就像上面显示的图像一样。

我不知道我的代码有什么问题,我搜索了但没有找到我的问题可能的参考。

这里是我的代码:

private void zipFiles(List<File> files, String directory) throws IOException 
{ 
    ZipOutputStream zos = null; 
    ZipEntry zipEntry = null; 
    FileInputStream fin = null; 
    FileOutputStream fos = null; 
    BufferedInputStream in = null; 

    String zipFileName = getZipFileName(); 

    try 
    { 
     fos = new FileOutputStream(File.separatorChar + zipFileName + EXTENSION); 

     zos = new ZipOutputStream(fos); 

     byte[] buf = new byte[1024]; 

     int len; 

     for(File file : files) 
     { 
      zipEntry = new ZipEntry(file.toString()); 
      fin = new FileInputStream(file); 
      in = new BufferedInputStream(fin); 
      zos.putNextEntry(zipEntry); 
      while ((len = in.read(buf)) >= 0) 
      { 
       zos.write(buf, 0, len); 
      } 
     } 
    } 
    catch(Exception e) 
    { 
     System.err.println("No fue posible zipear los archivos"); 
     e.printStackTrace(); 
    } 
    finally 
    { 
     in.close(); 
     zos.closeEntry(); 
     zos.close(); 
    } 

} 

希望你们能给我什么,我做错了,或我错过了什么暗示。

非常感谢。

顺便说一句,我给该方法的目录从来没有使用过。我给出的另一个参数是包含C:\ mantenimiento目录中所有文件和目录的文件列表。

+0

您能分享getZipFileName方法吗? – Sanj

+0

该方法实现非常简单:private String getZipFileName method() { \t String zipName; \t \t Date currentDate = newDate(); \t \t SimpleDateFormat formatter = SimpleDateFormat(“yyyy_MM_dd_HH-mm-ss”); \t \t zipName = formatter.format(currentDate); \t \t return zipName; \t } –

回答

0

我曾经有一个windows和zip文件的问题,其中创建的zip文件中没有包含文件夹的条目(即/,/Carpeta_A等),只有文件条目。尝试为没有流内容的文件夹添加ZipEntries。

但是,作为Java的某种庞大的Zip API的替代方案,您可以改用Filesystem(自Java7以来)。以下示例适用于Java8(lambda):

//Path pathToZip = Paths.get("path/to/your/folder"); 
//Path zipFile = Paths.get("file.zip"); 

public Path zipPath(Path pathToZip, Path zipFile) { 
    Map<String, String> env = new HashMap<String, String>() {{ 
     put("create", "true"); 
    }}; 

    try (FileSystem zipFs = FileSystems.newFileSystem(URI.create("jar:" + zipFile.toUri()), env)) { 
     Path root = zipFs.getPath("/"); 
     Files.walk(pathToZip).forEach(path -> zip(root, path)); 
    } 
} 

private static void zip(final Path zipRoot, final Path currentPath) { 

    Path entryPath = zipRoot.resolve(currentPath.toString()); 
    try { 
     Files.createDirectories(entryPath.getParent()); 
     Files.copy(currentPath, entryPath); 
    } catch (IOException e) { 
     throw new RuntimeException(e); 
    } 

} 
+0

我想用Java7尝试一下,因为我们建议您使用Java 6。但是,我必须尝试使用​​这种方法。“尝试为没有流式内容的文件夹添加ZipEntries。” –

+0

尝试在Windows中创建一个压缩文件,并使用类似7zip的工具或直接使用java打开它并列出条目。如果我不是完全错误的,那么应该有任何表示路径的条目,或者他们以类似的东西开始。但是,如何创建zip的条目以及Windows如何处理它(或者不是) –