无法使用Java创建用于压缩文件的文件夹? [英] Unable to make folders to zip files using java?

查看:94
本文介绍了无法使用Java创建用于压缩文件的文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我在Books文件夹中有一个文件夹(Books)结构,我有一个名为物理,化学,科学,english的文件夹。我将Books文件夹作为zipDeleteFile传递,但是在所有文件夹中,必须将相同的文件夹(Books)转换为physics.zip,chemistry.zip,science.zip,english.zip。但是此代码无法正常工作。

Here i have folder(Books)structure inside of Books folder i have folders called physics,chemistry,science,english.I'm passing Books folder as zipDeleteFile but inside all folder has to convert in the same folder(Books)as physics.zip,chemistry.zip,science.zip,english.zip.But this code is not working.

'

public void foldertToZip(File zipDeleteFile) {
    //System.out.println(zipDeleteFile);
    File directoryToZip = zipDeleteFile;
    List<File> fileList = new ArrayList<File>();
    //System.out.println("---Getting references to all files in: " + directoryToZip.getCanonicalPath());
    getAllFiles(directoryToZip, fileList);
    //System.out.println("---Creating zip file");
    writeZipFile(directoryToZip, fileList);
    //System.out.println("---Done");
}

public static void getAllFiles(File dir, List<File> fileList) {
    try {
        File[] files = dir.listFiles();
        for (File file : files) {
            fileList.add(file);
            if (file.isDirectory()) {
                System.out.println("directory:" + file.getCanonicalPath());
                getAllFiles(file, fileList);
            } else {
                System.out.println("     file:" + file.getCanonicalPath());
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void writeZipFile(File directoryToZip, List<File> fileList) {
    try {
        try (FileOutputStream fos = new FileOutputStream(directoryToZip.getName() + ".zip")) {
            ZipOutputStream zos = new ZipOutputStream(fos);

            for (File file : fileList) {
                if (!file.isDirectory()) { // we only zip files, not directories
                    addToZip(directoryToZip, file, zos);
                }
            }

            zos.close();
        }
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}

public static void addToZip(File directoryToZip, File file, ZipOutputStream zos) throws FileNotFoundException,
        IOException {
    try (FileInputStream fis = new FileInputStream(file)) {
        String zipFilePath = file.getCanonicalPath().substring(directoryToZip.getCanonicalPath().length() + 1,
                file.getCanonicalPath().length());
        System.out.println("Writing '" + zipFilePath + "' to zip file");
        ZipEntry zipEntry = new ZipEntry(zipFilePath);
        zos.putNextEntry(zipEntry);
        byte[] bytes = new byte[1024];
        int length;
        while ((length = fis.read(bytes)) >= 0) {
            zos.write(bytes, 0, length);
        }
        zos.closeEntry();
    }
}`'

最初,我将zipDeleteFile传递为C: Books书籍内的书籍我拥有所有物理,英语,科学文件夹,这些文件夹必须转换为同一根文件夹(书籍)中的zip文件。

Initially i'm passing zipDeleteFile as C:\Books inside Books i have all physics,english,science folder those folders has to convert into zip files in the same root folder(Books).

推荐答案

因此,基本上,您希望将 Books 文件夹中的每个目录压缩为自己的zip文件。您可以通过几种方法来完成此操作,但是最简单的方法可能是更改调用 foldertToZip

So, basically, you want to zip each of the directories in the Books folder into their own zip file. There is a couple of ways you could do this, but the eaiest might be to change the way you are calling foldertToZip

所以,而不是(类似)...

So, instead of (something like)...

foldertToZip(new File("C:\\Books"));

您可以执行以下操作...

You could do something like...

for (File file : new File("C:\\Books").listFiles()) {
    if (file.isDirectory()) {
        foldertToZip(file);
    }
}

这将导致每个目录在<$ c $之内c> Books 已添加到自己的zip文件中,该文件位于 Books

This will result in each directory within Books been added to it's own zip file, which will reside within Books

您可能还需要进行的另一项更改是...

One other change you might need to make is...

public static void writeZipFile(File directoryToZip, List<File> fileList) {
    try {
        //try (FileOutputStream fos = new FileOutputStream(directoryToZip.getName() + ".zip")) {
        File path = directoryToZip.getParentFile();
        File zipFile = new File(path, directoryToZip.getName() + ".zip");
        try (FileOutputStream fos = new FileOutputStream(zipFile)) {

这将创建zip要压缩目录的父目录中的文件(即 Books 目录)

This will create the zip file within the parent directory of the directory to be zipped (ie, the Books directory)

这篇关于无法使用Java创建用于压缩文件的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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