How do i read all the Zip files present in the folder and create a new text file with all the contents

user1694982

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.

user1694982

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);
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create zip file from all files in folder

From Dev

How to read all .txt files in a folder and append its contents into one .txt file, using Python?

From Dev

How do I delete all files in an Azure File Storage folder?

From Dev

How do I replace all files in a folder with one file?

From Dev

How do I remove all the lines in a text file that are present in another text file in Windows?

From Dev

Read all txt files in a specific folder and write all contents in one txt file

From Dev

Python to create multiple zip files for all in a folder

From Dev

How to list content of all .zip files in a folder and grep for a specific file?

From Dev

How do I unzip all files in a folder using 7-zip in batch?

From Dev

How do i read all the links present in a file using open function in Python?

From Dev

How to copy the contents of all files with a certain name into a new file?

From Dev

How can I read the contents of all the files in a directory with pandas?

From Dev

How do I create a zip folder of multiple files with the same filename, but that have different file extensions

From Java

How to read all ".java" files from a ".zip" file

From Dev

What are the linux commands to create a new folder & copy all the files & folders present in '/var/www/' into it to take backup of all code files?

From Dev

How to read files from every zip folder present in a directory

From Dev

How read all files in the folder and replace the pattern in file using Groovy

From Dev

show or read all files and folder present in os or system using electron

From Dev

create new file listing all text files in a directory with perl

From Dev

Bash - how do I wipe the contents of all files in a directory

From Dev

How to read all lines of text file to return if a value is present and then write this value if not present?

From Dev

Read two JSON files and extract all items present in the second file that are not in the first to a new object

From Dev

How to recursively replace a string in all files of a folder with the contents of specific file outside the folder using bash script?

From Dev

How do I grep a string on multiple files only if the string is present in all of all the files?

From Dev

How do you create a folder containing all the files in a project?

From Dev

How can I automatically open all text files in a given folder?

From Dev

How do I use grep to find all the zip files in a directory?

From Dev

How do i zip files in python without all directories written to it

From Dev

how to read all file contents in a directory with Laravel

Related Related

  1. 1

    Create zip file from all files in folder

  2. 2

    How to read all .txt files in a folder and append its contents into one .txt file, using Python?

  3. 3

    How do I delete all files in an Azure File Storage folder?

  4. 4

    How do I replace all files in a folder with one file?

  5. 5

    How do I remove all the lines in a text file that are present in another text file in Windows?

  6. 6

    Read all txt files in a specific folder and write all contents in one txt file

  7. 7

    Python to create multiple zip files for all in a folder

  8. 8

    How to list content of all .zip files in a folder and grep for a specific file?

  9. 9

    How do I unzip all files in a folder using 7-zip in batch?

  10. 10

    How do i read all the links present in a file using open function in Python?

  11. 11

    How to copy the contents of all files with a certain name into a new file?

  12. 12

    How can I read the contents of all the files in a directory with pandas?

  13. 13

    How do I create a zip folder of multiple files with the same filename, but that have different file extensions

  14. 14

    How to read all ".java" files from a ".zip" file

  15. 15

    What are the linux commands to create a new folder & copy all the files & folders present in '/var/www/' into it to take backup of all code files?

  16. 16

    How to read files from every zip folder present in a directory

  17. 17

    How read all files in the folder and replace the pattern in file using Groovy

  18. 18

    show or read all files and folder present in os or system using electron

  19. 19

    create new file listing all text files in a directory with perl

  20. 20

    Bash - how do I wipe the contents of all files in a directory

  21. 21

    How to read all lines of text file to return if a value is present and then write this value if not present?

  22. 22

    Read two JSON files and extract all items present in the second file that are not in the first to a new object

  23. 23

    How to recursively replace a string in all files of a folder with the contents of specific file outside the folder using bash script?

  24. 24

    How do I grep a string on multiple files only if the string is present in all of all the files?

  25. 25

    How do you create a folder containing all the files in a project?

  26. 26

    How can I automatically open all text files in a given folder?

  27. 27

    How do I use grep to find all the zip files in a directory?

  28. 28

    How do i zip files in python without all directories written to it

  29. 29

    how to read all file contents in a directory with Laravel

HotTag

Archive