Home > front end >  Extracted Zip file failed - No files inside, and having .zip25 extension after extract
Extracted Zip file failed - No files inside, and having .zip25 extension after extract

Time:01-23

I was trying to extract the ZIP file from my Linux, I'm able to extract it but the expected output is failing/wrong. The extract file suddenly no files inside and the folder extracted has .zip25 extension. I search on this, and there is saying that it is corrupted, however - I don't think it is corrupted because I am able to open and extract the zip files perfectly in local(windows directory).

Example:

Zip file: FolderZip.zip

After Extract: FolderZip.zip25 (Note: This is already extracted but still have .zip25 extension, also the files inside is missing).

Below is my code, I've worked on this for almost a month but still can't figure it out. Can someone help me to figure out what did I do wrong? Thank you :)

public void unZipFolder(String zipFile, String outputFolder){
        byte[] buffer = new byte[1024];

        System.out.println("ZipFileLocation: "   zipFile);
        LOG.info(" ZipFileLocation: "   zipFile);


            File folder = new File(outputFolder);
            if(!folder.exists())folder.mkdirs();

            try{
                
            FileInputStream fis = new FileInputStream(zipFile);
            ZipInputStream zis = new ZipInputStream(fis);
            ZipEntry ze = zis.getNextEntry();
            
            while(ze != null) {
                
                new File(folder.getParent()).mkdirs();
                FileOutputStream fos = new FileOutputStream(folder);
                File newFile = new File(outputFolder   FilenameUtils.indexOfLastSeparator(ze.getName()));
                
                                
                if (ze.isDirectory()) {
                    if (!newFile.isDirectory() && !newFile.mkdirs()) {
                        throw new IOException("Failed to create directory "   newFile);

                    }else if(ze.isDirectory()){
                        newFile.mkdirs();
                        continue;
                    }else{
                        
                        int len;
                        while ((len = zis.read(buffer)) >= 0) {
                            fos.write(buffer, 0, len);
                            }
                        
                        System.out.println("File Unzip: "   newFile);
                        LOG.info(" File Unzip: "   newFile);
                        newFile.mkdirs();
                        fos.close();
                        zis.closeEntry();
                        ze = zis.getNextEntry();
                    }
                }    
                
                boolean result = Files.deleteIfExists(Paths.get(zipFile));
                if (result) {
                    System.out.println("ZipFile is deleted....");
                } else {
                    System.out.println("Unable to delete the file.....");
              }
            }
            zis.closeEntry();
            zis.close();
            fis.close();

                
         }catch(IOException ex){
               ex.printStackTrace();
     }
}

CodePudding user response:

I'd love to be able to tell you exactly what's wrong with your code, but FileOutputStream fos = new FileOutputStream(folder); throws an exception because, well, folder is, a directory, so you can't write to it.

I'm also scratching my head over what your expecting new File(folder.getParent()).mkdirs(); to do.

I basically threw out your code and started again with...

public void unZipFolder(File zipFile, File outputFolder) throws IOException {
    byte[] buffer = new byte[1024];

    System.out.println("ZipFileLocation: "   zipFile);
    System.out.println("outputFolder = "   outputFolder);

    if (!outputFolder.exists() && !outputFolder.mkdirs()) {
        throw new IOException("Unable to create output folder: "   outputFolder);
    } else if (outputFolder.exists() && !outputFolder.isDirectory()) {
        throw new IOException("Output is not a directory: "   outputFolder);
    }

    try (ZipFile zipper = new ZipFile(zipFile)) {
        Enumeration<? extends ZipEntry> entries = zipper.entries();
        while (entries.hasMoreElements()) {
            ZipEntry ze = entries.nextElement();
            File destination = new File(outputFolder, ze.getName());
            if (ze.isDirectory()) {
                if (!destination.exists() && !destination.mkdirs()) {
                    throw new IOException("Could not create directory: "   destination);
                }
            } else {
                System.out.println("Writing "   destination);
                try (InputStream is = zipper.getInputStream(ze); FileOutputStream fos = new FileOutputStream(destination)) {
                    byte[] bytes = new byte[1024 * 4];
                    int bytesRead = -1;
                    while ((bytesRead = is.read(bytes)) != -1) {
                        fos.write(bytes, 0, bytesRead);
                    }
                }
            }
        }
    }
}

Now, what's important to know about this is, it expects the directory contents of the zip files to be relative (ie no root directory information). If your zip file does contain root directory information (ie C:/... or /...), then you're going to need to clean that up yourself.

Now, if you have trouble with this, I would suggest commenting out the "extraction" portion of the code and placing in more System.out.println statements

  • Related