0

Am new to Java, I searched in google for unzipping the files. Tested the code in my local and am able to unzip the files. But unable to delete the files I tried some logic but no luck. Can anyone help me how read a particular file and delete that file using its path and also need to delete a specific folder using its path and delete recursively. all the other files need to be there Below is the code:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class UnzipFiles {

    public static void main(String[] args) {
        String zipFilePath = "/Users/Safeer_Pasha/Music/archive.zip";
        
        String destDir = "/Workspace/";
        
        unzip(zipFilePath, destDir);
    }

    private static void unzip(String zipFilePath, String destDir) {
        File dir = new File(destDir);
        // create output directory if it doesn't exist
        if(!dir.exists()) dir.mkdirs();
        FileInputStream fis;
        //buffer for read and write data to file
        byte[] buffer = new byte[1024];
        try {
            fis = new FileInputStream(zipFilePath);
            ZipInputStream zis = new ZipInputStream(fis);
            ZipEntry ze = zis.getNextEntry();
            while(ze != null){
                String fileName = ze.getName();
                File newFile = new File(destDir + File.separator + fileName);
                System.out.println("Unzipping to "+newFile.getAbsolutePath());
                //create directories for sub directories in zip
                new File(newFile.getParent()).mkdirs();
                FileOutputStream fos = new FileOutputStream(newFile);
                int len;
                while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
                }
                fos.close();
                //close this ZipEntry
                zis.closeEntry();
                ze = zis.getNextEntry();

                

            }
            //close last ZipEntry
            zis.closeEntry();
            zis.close();
            fis.close();

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

}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Safeer Pasha
  • 19
  • 1
  • 6
  • Am not from the development background please do help me, am trying to learn. – Safeer Pasha Jul 07 '22 at 12:19
  • 1
    Could you add your code related to deleting the files and which error you get? Are you sure that you have the sufficient permissions to delete such files? – kamaci Jul 07 '22 at 12:21
  • How is it not working? Do you get an error? Which is the line or section that is supposed to delete the files? – Lajos Arpad Jul 07 '22 at 12:42
  • Does this answer your question? [Java 8 - Recursive delete of folders according to predicate](https://stackoverflow.com/questions/49373994/java-8-recursive-delete-of-folders-according-to-predicate) – Bagus Tesa Jul 07 '22 at 12:57
  • I have sufficient permission , but I do not know how write a code for reading the files and deleting it. Am naive to development – Safeer Pasha Jul 07 '22 at 13:39
  • How to read a new directory where I have unzip the file and search for particular file using the path – Safeer Pasha Jul 07 '22 at 13:41
  • The code in your question simply unpacks a ZIP file. I see no code that deletes files. Am I missing something? If not, then I understand that you are asking how to write Java code that deletes files and directories. Is that correct? Do you know the path to the files and directories that you wish to delete? – Abra Jul 07 '22 at 14:18
  • If you can't delete a file on Windows, it could also mean that it is still open somewhere (maybe even in your own application), given you're not using try-with-resources, it is entirely possible if exception have occurred that some resources (e.g. files) haven't been closed. – Mark Rotteveel Jul 07 '22 at 15:28
  • Hello @Abra, Exactly that was my question, I have no idea how to delete the files and sub folders present in the zip file after unzipping. Yes I know the path of files and sub folders which I need to delete. Could you please help me writing the deletion code to above script. If could please also add the comments to code you have written. Thanks in advance – Safeer Pasha Jul 08 '22 at 10:29
  • @Abra, I dont know how to declare a file and read it with path so that I can find that using for loop. Same goes with sub folders which I have to delete using for loop. – Safeer Pasha Jul 08 '22 at 10:33
  • I don't understand. Do you want to remove files from the ZIP? Or do you want to delete the files after you extract them from the ZIP? Does this help? https://docs.oracle.com/javase/tutorial/essential/io/delete.html – Abra Jul 08 '22 at 14:46

0 Answers0