Java - 使用子文件夹

时间:2017-11-16 12:17:53

标签: java zip subdirectory multiple-files

我正在尝试用Java生成一个zip文件,其中包含几个位于不同位置的不同类型的文件(例如图像,字体等)。此外,我希望zip文件具有子文件夹,文件按类型放置(例如图像应该转到zip中的images文件夹。

这些是我拥有的文件(每个文件可以位于不同的位置):

  • 的index.html
  • img1.jpg
  • img2.jpg
  • font1.woff
  • font2.woff
  • 的style.css
  • custom.js

这就是他们应该在zip文件中的方式:

  • 的index.html
  • 图像/ img1.jpg
  • 图像/ img2.jpg
  • 字体/ font1.woff
  • 字体/ font2.woff
  • JS​​ / custom.js
  • CSS / Styles.css中

到目前为止,我已设法在特定路径中获取一个文件并提示用户输出输出位置。将使用输入中指定的文件生成zip文件。这是我到目前为止的代码:

JFrame parentFrame = new JFrame();

JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Speicherort auswählen");   

int userSelection = fileChooser.showSaveDialog(parentFrame);
String pathToFile;

if (userSelection == JFileChooser.APPROVE_OPTION) {
    File fileToSave = fileChooser.getSelectedFile();
    print(fileToSave.getAbsolutePath());
    pathToFile = fileToSave.getAbsolutePath();
}

pathToFile = pathToFile.replace("\\", "/");

String outFileName = pathToFile;
String inFileName = "C:/Users/asoares/Desktop/mobio_export_test/index.html";
ZipOutputStream zos = null; 
FileInputStream fis = null;

try {
    zos = new ZipOutputStream(new FileOutputStream(outFileName));
    fis = new FileInputStream(inFileName);
    zos.putNextEntry(new ZipEntry(new File(inFileName).getName()));
    int len;
    byte[] buffer = new byte[2048];
    while((len = fis.read(buffer, 0, buffer.length)) > 0) {
        zos.write(buffer, 0, len);
    }
} catch (FileNotFoundException e) { 
    e.printStackTrace(); 
} catch (IOException e) { 
    e.printStackTrace(); 
} finally { 
    if(fis != null){ 
        try { 
            fis.close(); 
        } catch (IOException e) {} 
    } 
    if(zos != null){ 
        try { 
            zos.closeEntry(); 
            zos.close(); 
        } catch (IOException e) {} 
    } 
}

如果有人可以帮助我,我会很高兴!!!

1 个答案:

答案 0 :(得分:0)

它应该像这样工作。

zip目录名称最多应由另一种方法创建(图像类型多于jpg:)。)

public static Path zip(List<Path> files, Path zipFileTarget) throws IOException {
    try (FileOutputStream fos = new FileOutputStream(zipFileTarget.toFile());
         ZipOutputStream zos = new ZipOutputStream(fos)) {
        if (!Files.exists(zipFileTarget))
            Files.createFile(zipFileTarget);
        createEntries(files, zos);
        zos.close();
        return zipFileTarget;
    }
}

private static List<String> createEntries(List<Path> files, ZipOutputStream zos) throws IOException {
    List<String> zippedFiles = new ArrayList<>();
    Matcher matcherFileExt = Pattern.compile("^.*\\.([^.]+)$").matcher("");
    for (Path f : files) {
        if (Files.isRegularFile(f)) {
            String fileName = f.getFileName().toString();
            String fileExt = matcherFileExt.reset(fileName).matches()
                    ? matcherFileExt.replaceAll("$1")
                    : "unknown";
            // You should determine the dir name with a more sophisticated
            // approach.
            String dir;
            if      (fileExt.equals("jpg")) dir = "images";
            else if (fileExt.equals("woff")) dir = "fonts";
            else    dir = fileExt;

            zos.putNextEntry(new ZipEntry(dir + "/" + fileName));
            Files.copy(f, zos);
            zippedFiles.add(fileName);
        }
    }
    return zippedFiles;
}

编辑:这种方法适用于java 1.7+。您可以通过调用File方法轻松地将Path对象转换为toPath()对象。