0

I am using following code to create a backup of a folder. The following code creates a zip file, but I am unable to unzip the created file. Please help me identify a bug in the following code:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.bakeit.Configurations;

public class BakeSiteActionFacade {

    public void prepareDestination() throws IOException {

        if (!Paths.get(Configurations.BACKUP_PATH).toFile().exists()) {
                Files.createDirectories(Paths.get(Configurations.BACKUP_PATH));
        }
        File destinationFolder = null;
        zipDirectory(destinationFolder, Configurations.BACKUP_PATH
                + File.separator + "backup-"
                + new Date().toString().replaceAll(":", "").replaceAll(" ", "")
                + ".zip");
    }

    List<String> filesListInDir = new ArrayList<String>();

    private void zipDirectory(File dir, String zipDirName) {
        try {
            populateFilesList(dir);
            FileOutputStream fos = new FileOutputStream(zipDirName);
            ZipOutputStream zos = new ZipOutputStream(fos);
            for (String filePath : filesListInDir) {
                System.out.println("Zipping " + filePath);
                ZipEntry ze = new ZipEntry(filePath.substring(dir
                        .getAbsolutePath().length() + 1, filePath.length()));
                zos.putNextEntry(ze);
                FileInputStream fis = new FileInputStream(filePath);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, len);
                }
                zos.closeEntry();
                fis.close();
            }
            zos.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void populateFilesList(File dir) throws IOException {
        File[] files = dir.listFiles();
        for (File file : files) {
            if (file.isFile())
                filesListInDir.add(file.getAbsolutePath());
            else
                populateFilesList(file);
        }
    }

}

PS: Configurations.BACKUP_PATH is a String which contains valid path to backup folder, which may or may not exist.

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
  • 2
    Your code works fine for me but maybe it is about a limitation of zip format about the number of files it can handle if you don't use at least java 7 : http://stackoverflow.com/questions/6738773/zip-files-with-java-is-there-a-limit. – Aurelien Jan 02 '16 at 16:14
  • can you post the error/exception you get when unzipping the file – guleryuz Jan 03 '16 at 07:24
  • yes, @Aurelien . That is the root cause. thanks. Marked as duplicate. – Mohit Kanwar Jan 04 '16 at 07:30

0 Answers0