From ba06e0560c36db4ef5cebebe8a8abec27fcb3a93 Mon Sep 17 00:00:00 2001 From: Thiluxan Subramaniam Date: Wed, 6 Oct 2021 09:46:13 +0530 Subject: [PATCH] Added Zip File Handler Java Program --- JAVA/ZipFileHandler.java | 84 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 JAVA/ZipFileHandler.java diff --git a/JAVA/ZipFileHandler.java b/JAVA/ZipFileHandler.java new file mode 100644 index 00000000..c5df33c7 --- /dev/null +++ b/JAVA/ZipFileHandler.java @@ -0,0 +1,84 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.Enumeration; +import java.util.stream.Collectors; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +public class ZipFileHandler { + + public void listZipFileContents(String filePath) throws Exception{ + ZipFile zipFile = null; + try{ + zipFile = new ZipFile(filePath); + Enumeration enumeration = zipFile.entries(); + while (enumeration.hasMoreElements()){ + ZipEntry zipEntry = enumeration.nextElement(); + System.out.println(zipEntry.getName()); + } + } + catch (Exception e){ + System.out.println("Error: "+e); + } + finally { + try { + if(zipFile != null) { + zipFile.close(); + } + } + catch (Exception e){ + System.out.println("Error: "+e); + } + } + } + + public void searchAndReadInZipEntity(String searchPath, String zipFilePath){ + ZipFile zipFile = null; + + boolean fileFound = false; + + try { + + zipFile = new ZipFile(zipFilePath); + + Enumeration e = zipFile.entries(); + + while (e.hasMoreElements()) { + + ZipEntry entry = e.nextElement(); + + // get the name of the entry + String entryName = entry.getName(); + + if (entryName.equalsIgnoreCase(searchPath)) { + fileFound = true; + InputStream inputStream = zipFile.getInputStream(entry); + String result = new BufferedReader(new InputStreamReader(inputStream)) + .lines().collect(Collectors.joining("\n")); + System.out.println(result); + break; + } + + } + + } + catch (IOException ioe) { + System.out.println("Error opening zip file" + ioe); + } + finally { + try { + if (zipFile!=null) { + zipFile.close(); + } + } + catch (IOException ioe) { + System.out.println("Error while closing zip file" + ioe); + } + } + + System.out.println("File found: " + fileFound); + + } +}