<java> Read Zip file and convert directly to string

Overview

Read the Zip file and convert it directly to a string.

Commentary

The readAllBytes method of the InputStream class is used for reading. It's very convenient because it reads all the bytes at once.

However, according to the reference, "Note that this method is used in a simple case where it is convenient to read all the bytes into one byte array, because it reads an input stream with a large amount of data. It is not the one. "

code

Unzip.java



import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.util.zip.ZipInputStream;

public class Unzip{
	public static void main(String[] args) throws Exception {

		String filePath = "Specify the Zip file path here";

		ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(filePath)));

		//Repeat as long as there is a file in the zip file
		while(zis.getNextEntry() !=null){

			String str = new String(zis.readAllBytes());

			//Output for the time being
			System.out.println(str);
		}
		zis.close();
	}
}


reference

For reference, I'll also include the code to read little by little.

Sample.java


		String filePath = "Specify the path of the Zip file to read here";

		ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(filePath)));


		String str = null;
		byte[] tempBuffer = new byte[1024];//Number of bytes to read at one time
		ByteArrayOutputStream streamBuilder = null;
		int bytesRead;

		//Repeat as long as there is a file in the zip file
		while(zis.getNextEntry() !=null){
			while ( (bytesRead = zis.read(tempBuffer)) != -1 ){

				streamBuilder = new ByteArrayOutputStream();
				streamBuilder.write(tempBuffer, 0, bytesRead);

			}
           //Convert to String
			str = streamBuilder.toString("UTF-8");
			System.out.println(str);
		}
		zis.close();

Recommended Posts

<java> Read Zip file and convert directly to string
[Java] Convert Object type null to String type
Sample to read and write LibreOffice Calc fods file in Java 2021
[Java] How to convert from String to Path type and get the path
Convert Excel to Blob with java, save it, read it from DB and output it as a file!
Read a string in a PDF file with Java
[Java] Convert and import file values with OpenCSV
Read Java Property file
[Java] String comparison and && and ||
How to ZIP a JAVA CSV file and manage it in a Byte array
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 3
Convert Java enum enums and JSON to and from Jackson
Sample code to convert List to List <String> in Java Stream
[Java] Convert character strings to uppercase / lowercase (AOJ‚Ď®-swap uppercase and lowercase)
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 2
Java memory management and how to read GC Viewer
Mastering Kotlin ~ Convert Java File to Kotlin File Road to Graduation ~ Part 1
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
[Java] Convert JSON to Java and Java to JSON-How to use GSON and Jackson-
Gzip-compress byte array in Java and output to file
Convert Java Powerpoint to XPS
Convert String type to Timestamp type
Convert to Ruby Leet string
[Java] About String and StringBuilder
[Java] [Android] Read ini file
How to convert Java radix
[Java] Convert ArrayList to array
How to read your own YAML file (*****. Yml) in Java
Generate and execute Jar file of Java file belonging to package
Convert a Java byte array to a string in hexadecimal notation
[Java] How to convert a character string from String type to byte type
I tried to convert a string to a LocalDate type in Java
Read Java properties file in C #
How to convert erb file to haml
Unzip the zip file in Java
I want to convert InputStream to String
How to read a file and treat it as standard input
Log output to file in Java
[Java] Convert 1-to-N List to Map
How to convert LocalDate and Timestamp
Convert iso-2022-jp character string to utf-8
Read the first 4 bytes of the Java class file and output CAFEBABE
[Convenient to remember !!!] How to convert from LocalDate type to character string and from character string to LocalDate type
Sample to read and write LibreOffice Calc fods file in JRuby 2021
I tried to summarize the methods of Java String and StringBuilder
[Android] Convert Map to JSON using GSON in Kotlin and Java
Studying Java 8 (String Joiner and join)
[Android] Convert Android Java code to Kotlin
[Java] Convert array to ArrayList * Caution
The story of forgetting to close a file in Java and failing
To create a Zip file while grouping database search results in Java
How to record JFR (Java Flight Recorder) and output a dump file
How to read log4j configuration file in Java project summarized in jar file Memo
Convert Java org.w3c.dom.Document objects and XML strings
[Java] How to use the File class
Convert all Android apps (Java) to Kotlin
Convert from java UTC time to JST time
Java implementation to create and solve mazes
[Java] How to output and write files!
How to output Java string to console screen