0

I am able to Read only one zip file but not all present in the folder and below is the code: i will be reading most of the files using this technique, please help me

import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.imageio.ImageIO;

public class ZipReader 
{

    public static void main(String[] args) throws IOException 
    {


        ZipFile ZipFile = new ZipFile("PATH");
        Enumeration<? extends ZipEntry> entries = ZipFile.entries();

        while(entries.hasMoreElements()){
            ZipEntry entry = entries.nextElement();
            InputStream stream = ZipFile.getInputStream(entry);
            InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
            Scanner inputStream = new Scanner(reader);
            inputStream.nextLine();

            while (inputStream.hasNext()) {
                String data = inputStream.nextLine(); // Gets a whole line
                System.out.println(data);
            }

            inputStream.close();
            stream.close();
        }
        ZipFile.close();


    }


}

After code runs i am getting output for one file not that i am inserting into file but i am printing all the file details in console.

Harish
  • 27
  • 3
  • You want to have the content of the zip in one single output file? Then just create one single output file outside of `while` and write to it like presented in any of the [available examples](https://www.google.com/search?q=example+unzip+java&ie=utf-8&oe=utf-8&client=firefox-b-e) – Curiosa Globunznik Nov 22 '19 at 04:33
  • [Using File.listFiles with FileNameExtensionFilter](https://stackoverflow.com/questions/5751335/using-file-listfiles-with-filenameextensionfilter); [Java: List Files in a Directory](https://stackabuse.com/java-list-files-in-a-directory/); [Java example to filter files in a directory using FilenameFilter](https://www.codevscolor.com/java-example-filter-files-in-directory-filenamefilter/); [How to list all files in a directory?](https://www.mkyong.com/java/java-how-to-list-all-files-in-a-directory/) – MadProgrammer Nov 22 '19 at 04:42

1 Answers1

-1

Thanks for referring the links i found an resolution:

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UnZipFiles {
    private static final Logger _LOGGER = LoggerFactory.getLogger(UnZipFiles.class);

    public void unzipAll(String path) {
        String filName;
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles(); 
        for (int i = 0; i < listOfFiles.length; i++) {
            if (listOfFiles[i].isFile()) {
                filName = listOfFiles[i].getName();
                if (filName.endsWith(".zip") || filName.endsWith(".ZIP")) {
                    unZipFile(listOfFiles[i]);
                }
            }
        }
    }

    public static void main(String[] args) {

        UnZipFiles U = new UnZipFiles();
        U.unzipAll("PATH");

    }

    public void unZipFile(File zipFile) {
        byte[] buffer = new byte[1024];

        try {
            // create output directory is not exists
            File folder = new File("PATH");
            if (!folder.exists()) {
                folder.mkdir();
            }

            // get the zip file content
            ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
            // get the zipped file list entry
            ZipEntry ze = zis.getNextEntry();

            while (ze != null) {
               String fileName = ze.getName();
               File newFile = new File("PATH" + File.separator + fileName);

               if (_LOGGER.isDebugEnabled())
                   _LOGGER.debug("Unzipping file : {}", new Object[] {newFile.getAbsoluteFile()});

                // create all non exists folders
                // else you will hit FileNotFoundException for compressed folder
                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();   
                ze = zis.getNextEntry();
            }
            zis.closeEntry();
            zis.close();
        } catch(IOException ex) {
            _LOGGER.error("Exception occurred while unzipping!", ex);
        }
    }
}
Harish
  • 27
  • 3