Unable to make folders to zip files using java?

爷,独闯天下 提交于 2019-12-23 06:05:17

问题


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

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).


回答1:


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

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)) {

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



来源:https://stackoverflow.com/questions/24006655/unable-to-make-folders-to-zip-files-using-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!