简体   繁体   中英

Unable to make folders to zip files using java?

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM