java.util.zip - 重新创建目录结构

时间:2009-09-09 11:31:39

标签: java directory zip structure

在尝试使用java.util.zip压缩存档时,我遇到了很多问题,我解决了大部分问题。现在我终于得到了一些输出,我很难获得“正确”的输出。我有一个提取的ODT文件(目录将更符合描述)我做了一些修改。现在我想压缩该目录以重新创建ODT文件结构。压缩目录并将其重命名为以.odt结尾工作正常,因此应该没有问题。

主要问题是我丢失了目录的内部结构。一切都变得“平坦”,我似乎找不到保留原始多层结构的方法。我希望得到一些帮助,因为我似乎无法找到问题。

以下是相关的代码段:

ZipOutputStream out = new ZipOutputStream(new FileOutputStream(
    FILEPATH.substring(0, FILEPATH.lastIndexOf(SEPARATOR) + 1).concat("test.zip")));
    compressDirectory(TEMPARCH, out);

SEPARATOR是系统文件分隔符,FILEPATH是原始ODT的文件路径,我将覆盖但尚未在此处进行测试。我只是写入同一目录中的test.zip文件。

private void compressDirectory(String directory, ZipOutputStream out) throws IOException
{
    File fileToCompress = new File(directory);
    // list contents.
    String[] contents = fileToCompress.list();
    // iterate through directory and compress files.
    for(int i = 0; i < contents.length; i++)
    {
        File f = new File(directory, contents[i]);
        // testing type. directories and files have to be treated separately.
        if(f.isDirectory())
        {
            // add empty directory
            out.putNextEntry(new ZipEntry(f.getName() + SEPARATOR));
            // initiate recursive call
            compressDirectory(f.getPath(), out);
            // continue the iteration
            continue;
        }else{
             // prepare stream to read file.
             FileInputStream in = new FileInputStream(f);
             // create ZipEntry and add to outputting stream.
             out.putNextEntry(new ZipEntry(f.getName()));
             // write the data.
             int len;
             while((len = in.read(data)) > 0)
             {
                 out.write(data, 0, len);
             }
             out.flush();
             out.closeEntry();
             in.close();
         }
     }
 }

包含要压缩文件的目录位于用户空间中的某个位置,而不是与生成的文件位于同一目录中。我认为这可能是麻烦但我不知道如何。此外,我认为问题可能是使用相同的流输出,但我再也看不出如何。我在一些示例和教程中看到他们使用getPath()而不是getName(),但更改它会给我一个空的zip文件。

8 个答案:

答案 0 :(得分:92)

URI类对于处理相对路径非常有用。

File mydir = new File("C:\\mydir");
File myfile = new File("C:\\mydir\\path\\myfile.txt");
System.out.println(mydir.toURI().relativize(myfile.toURI()).getPath());

上面的代码将发出字符串path/myfile.txt

为了完整性,这里有一个用于归档目录的zip方法:

  public static void zip(File directory, File zipfile) throws IOException {
    URI base = directory.toURI();
    Deque<File> queue = new LinkedList<File>();
    queue.push(directory);
    OutputStream out = new FileOutputStream(zipfile);
    Closeable res = out;
    try {
      ZipOutputStream zout = new ZipOutputStream(out);
      res = zout;
      while (!queue.isEmpty()) {
        directory = queue.pop();
        for (File kid : directory.listFiles()) {
          String name = base.relativize(kid.toURI()).getPath();
          if (kid.isDirectory()) {
            queue.push(kid);
            name = name.endsWith("/") ? name : name + "/";
            zout.putNextEntry(new ZipEntry(name));
          } else {
            zout.putNextEntry(new ZipEntry(name));
            copy(kid, zout);
            zout.closeEntry();
          }
        }
      }
    } finally {
      res.close();
    }
  }

这段代码不会保留日期,我不知道它会如何对符号链接这样的东西做出反应。没有尝试添加目录条目,因此不会包含空目录。

相应的unzip命令:

  public static void unzip(File zipfile, File directory) throws IOException {
    ZipFile zfile = new ZipFile(zipfile);
    Enumeration<? extends ZipEntry> entries = zfile.entries();
    while (entries.hasMoreElements()) {
      ZipEntry entry = entries.nextElement();
      File file = new File(directory, entry.getName());
      if (entry.isDirectory()) {
        file.mkdirs();
      } else {
        file.getParentFile().mkdirs();
        InputStream in = zfile.getInputStream(entry);
        try {
          copy(in, file);
        } finally {
          in.close();
        }
      }
    }
  }

他们依赖的实用方法:

  private static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    while (true) {
      int readCount = in.read(buffer);
      if (readCount < 0) {
        break;
      }
      out.write(buffer, 0, readCount);
    }
  }

  private static void copy(File file, OutputStream out) throws IOException {
    InputStream in = new FileInputStream(file);
    try {
      copy(in, out);
    } finally {
      in.close();
    }
  }

  private static void copy(InputStream in, File file) throws IOException {
    OutputStream out = new FileOutputStream(file);
    try {
      copy(in, out);
    } finally {
      out.close();
    }
  }

缓冲区大小完全是任意的。

答案 1 :(得分:7)

我在你的代码中看到了2个问题,

  1. 您不保存目录路径,因此无法将其恢复。
  2. 在Windows上,您需要使用“/”作为路径分隔符。一些解压缩程序不喜欢\。
  3. 我包含我自己的版本供您参考。我们使用这个来压缩照片下载所以它适用于各种解压缩程序。它保留了目录结构和时间戳。

      public static void createZipFile(File srcDir, OutputStream out,
       boolean verbose) throws IOException {
    
      List<String> fileList = listDirectory(srcDir);
      ZipOutputStream zout = new ZipOutputStream(out);
    
      zout.setLevel(9);
      zout.setComment("Zipper v1.2");
    
      for (String fileName : fileList) {
       File file = new File(srcDir.getParent(), fileName);
       if (verbose)
        System.out.println("  adding: " + fileName);
    
       // Zip always use / as separator
       String zipName = fileName;
       if (File.separatorChar != '/')
        zipName = fileName.replace(File.separatorChar, '/');
       ZipEntry ze;
       if (file.isFile()) {
        ze = new ZipEntry(zipName);
        ze.setTime(file.lastModified());
        zout.putNextEntry(ze);
        FileInputStream fin = new FileInputStream(file);
        byte[] buffer = new byte[4096];
        for (int n; (n = fin.read(buffer)) > 0;)
         zout.write(buffer, 0, n);
        fin.close();
       } else {
        ze = new ZipEntry(zipName + '/');
        ze.setTime(file.lastModified());
        zout.putNextEntry(ze);
       }
      }
      zout.close();
     }
    
     public static List<String> listDirectory(File directory)
       throws IOException {
    
      Stack<String> stack = new Stack<String>();
      List<String> list = new ArrayList<String>();
    
      // If it's a file, just return itself
      if (directory.isFile()) {
       if (directory.canRead())
        list.add(directory.getName());
       return list;
      }
    
      // Traverse the directory in width-first manner, no-recursively
      String root = directory.getParent();
      stack.push(directory.getName());
      while (!stack.empty()) {
       String current = (String) stack.pop();
       File curDir = new File(root, current);
       String[] fileList = curDir.list();
       if (fileList != null) {
        for (String entry : fileList) {
         File f = new File(curDir, entry);
         if (f.isFile()) {
          if (f.canRead()) {
           list.add(current + File.separator + entry);
          } else {
           System.err.println("File " + f.getPath()
             + " is unreadable");
           throw new IOException("Can't read file: "
             + f.getPath());
          }
         } else if (f.isDirectory()) {
          list.add(current + File.separator + entry);
          stack.push(current + File.separator + f.getName());
         } else {
          throw new IOException("Unknown entry: " + f.getPath());
         }
        }
       }
      }
      return list;
     }
    }
    

答案 2 :(得分:4)

只需浏览java.util.zip.ZipEntry的源代码即可。如果ZipEntry的名称以“/”字符结尾,则将其视为目录。只需在目录名后加上“/”。 您还需要删除驱动器前缀以使其相对。

检查此示例是否只压缩空目录

http://bethecoder.com/applications/tutorials/showTutorials.action?tutorialId=Java_ZipUtilities_ZipEmptyDirectory

只要您能够创建空和&amp; ZIP文件中的非空目录,您的目录结构完好无损。

祝你好运。

答案 3 :(得分:3)

这是另一个示例(递归),它还允许您从zip包含/排除包含文件夹:

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;

public class ZipUtil {

  private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

  public static void main(String[] args) throws Exception {
    zipFile("C:/tmp/demo", "C:/tmp/demo.zip", true);
  }

  public static void zipFile(String fileToZip, String zipFile, boolean excludeContainingFolder)
    throws IOException {        
    ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));    

    File srcFile = new File(fileToZip);
    if(excludeContainingFolder && srcFile.isDirectory()) {
      for(String fileName : srcFile.list()) {
        addToZip("", fileToZip + "/" + fileName, zipOut);
      }
    } else {
      addToZip("", fileToZip, zipOut);
    }

    zipOut.flush();
    zipOut.close();

    System.out.println("Successfully created " + zipFile);
  }

  private static void addToZip(String path, String srcFile, ZipOutputStream zipOut)
    throws IOException {        
    File file = new File(srcFile);
    String filePath = "".equals(path) ? file.getName() : path + "/" + file.getName();
    if (file.isDirectory()) {
      for (String fileName : file.list()) {             
        addToZip(filePath, srcFile + "/" + fileName, zipOut);
      }
    } else {
      zipOut.putNextEntry(new ZipEntry(filePath));
      FileInputStream in = new FileInputStream(srcFile);

      byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
      int len;
      while ((len = in.read(buffer)) != -1) {
        zipOut.write(buffer, 0, len);
      }

      in.close();
    }
  }
}

答案 4 :(得分:2)

如果您不想打扰处理字节输入流,缓冲区大小和其他低级别细节。您可以使用Java代码中的Ant的Zip库(可以找到maven依赖项here)。现在,我制作一个包含文件列表的邮政编码。目录:

public static void createZip(File zipFile, List<String> fileList) {

    Project project = new Project();
    project.init();

    Zip zip = new Zip();
    zip.setDestFile(zipFile);
    zip.setProject(project);

    for(String relativePath : fileList) {

        //noramalize the path (using commons-io, might want to null-check)
        String normalizedPath = FilenameUtils.normalize(relativePath);

        //create the file that will be used
        File fileToZip = new File(normalizedPath);
        if(fileToZip.isDirectory()) {
            ZipFileSet fileSet = new ZipFileSet();
            fileSet.setDir(fileToZip);
            fileSet.setPrefix(fileToZip.getPath());
            zip.addFileset(fileSet);
        } else {
            FileSet fileSet = new FileSet();
            fileSet.setDir(new File("."));
            fileSet.setIncludes(normalizedPath);
            zip.addFileset(fileSet);
        }
    }

    Target target = new Target();
    target.setName("ziptarget");
    target.addTask(zip);
    project.addTarget(target);
    project.executeTarget("ziptarget");
}

答案 5 :(得分:1)

我想在此处添加建议/提醒:

如果您将输出目录定义为与输入目录相同,则需要将每个文件的名称与输出.zip的文件名进行比较,以避免压缩文件本身,从而产生一些不需要的行为。希望这有任何帮助。

答案 6 :(得分:1)

要在Windows中压缩文件夹及其子文件夹的内容,

替换,

out.putNextEntry(new ZipEntry(files[i])); 

out.putNextEntry(new ZipEntry(files[i]).replace(inFolder+"\\,"")); 

答案 7 :(得分:0)

此代码剪辑适用于我。不需要第三方库。

public static void zipDir(final Path dirToZip, final Path out) {
    final Stack<String> stackOfDirs = new Stack<>();
    final Function<Stack<String>, String> createPath = stack -> stack.stream().collect(Collectors.joining("/")) + "/";
    try(final ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(out.toFile()))) {
        Files.walkFileTree(dirToZip, new FileVisitor<Path>() {

            @Override
            public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs) throws IOException {
                stackOfDirs.push(dir.toFile().getName());
                final String path = createPath.apply(stackOfDirs);
                final ZipEntry zipEntry = new ZipEntry(path);
                zipOut.putNextEntry(zipEntry);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
                final String path = String.format("%s%s", createPath.apply(stackOfDirs), file.toFile().getName());
                final ZipEntry zipEntry = new ZipEntry(path);
                zipOut.putNextEntry(zipEntry);
                Files.copy(file, zipOut);
                zipOut.closeEntry();
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(final Path file, final IOException exc) throws IOException {
                final StringWriter stringWriter = new StringWriter();
                try(final PrintWriter printWriter = new PrintWriter(stringWriter)) {
                    exc.printStackTrace(printWriter);
                    System.err.printf("Failed visiting %s because of:\n %s\n",
                            file.toFile().getAbsolutePath(), printWriter.toString());
                }
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
                stackOfDirs.pop();
                return FileVisitResult.CONTINUE;
            }
        });
    } catch (IOException e) {
        e.printStackTrace();
    }
}