Search code examples
javazipunzip

Unzipping(extracting zip)file showing error,unexpected end of archive


Here i have Bookfolder in that few more folders(english,hindi,japanese).Converting english,hindi,japanese to english.zip,hindi.zip and japanese.zip.Everything is working fine and i'm keeping zip files and folders inside Bookfolder,this thing i'm doing using with java.But when i'm unzipping manually the zip file ex:english.zip ,right click on that extract here then showing error as UNEXPECTED END OF ARCHIVE.This is my code.

public void foldertToZip(File zipDeleteFile) {
    //System.out.println(zipDeleteFile);
    File directoryToZip = zipDeleteFile;
    List<File> fileList = new ArrayList<>();
    //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) {
    }
}

public static void writeZipFile(File directoryToZip, List<File> fileList) {
    try {
        //try (FileOutputStream fos = new FileOutputStream(directoryToZip.getName() + ".zip"); ZipOutputStream zos = new ZipOutputStream(fos)) {
        File path = directoryToZip.getParentFile();
        File zipFile = new File(path, directoryToZip.getName() + ".zip");
        try (FileOutputStream fos = new FileOutputStream(zipFile)) {
            ZipOutputStream zos = new ZipOutputStream(fos);
            for (File file : fileList) {
                if (!file.isDirectory()) { // we only zip files, not directories
                    addToZip(directoryToZip, file,zos);
                }
            }
        }
    } 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();
    }
}`

while i'm extracting new zip file(eg:english.zip) it showing error as unexpected end of archieve (i think not zipping exactly)'


Solution

  • You need to close ZipOutputStream in writeZipFile() method;

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