java.util.zip - 重新创建目录结构 [英] java.util.zip - Recreating directory structure

查看:29
本文介绍了java.util.zip - 重新创建目录结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While trying to zip an archive using the java.util.zip I ran into a lot of problems most of which I solved. Now that I finally get some output I struggle with getting the "right" output. I have an extracted ODT file (directory would be more fitting a description) to which I did some modifications. Now I want to compress that directory as to recreate the ODT file structure. Zipping the directory and renaming it to end with .odt works fine so there should be no problem.

主要问题是我丢失了目录的内部结构.一切都变得平坦",我似乎没有找到保留原始多层结构的方法.我很感激这方面的帮助,因为我似乎找不到问题.

The main problem is that I lose the internal structure of the directory. Everything becomes "flat" and I do not seem to find a way to preserve the original multi-layered structure. I would appreciate some help on this as I can not seem to find the problem.

以下是相关代码片段:

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

SEPARATOR 是系统文件分隔符,FILEPATH 是原始 ODT 的文件路径,我将覆盖它,但为了测试目的,这里没有这样做.我只是写入同一目录中的 test.zip 文件.

The SEPARATOR is the system file separator and the FILEPATH is the filepath of the original ODT which I will override but have not done here for testing purposes. I simply write to a test.zip file in the same directory.

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 文件.

The directory that contains the files to zip is somewhere in the user space and not in the same directory as the resulting file. I assume this could be trouble but I can not really see how. Also I figured that the problem could be in using the same stream for outputting but again I can not see how. I saw in some examples and tutorials that they use getPath() instead of getName() but changing that gives me an empty zip file.

推荐答案

URI 类对于处理相对路径很有用.

The URI class is useful for working with relative paths.

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.

The above code will emit the string path/myfile.txt.

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

For completeness, here is a zip method for archiving a directory:

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

这段代码不保留日期,我不确定它对符号链接之类的东西会有什么反应.不会尝试添加目录条目,因此不会包含空目录.

This code makes doesn't preserve dates and I'm not sure how it would react to stuff like symlinks. No attempt is made to add directory entries, so empty directories would not be included.

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

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

The buffer size is entirely arbitrary.

这篇关于java.util.zip - 重新创建目录结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆