read a zip file in java code example
Example 1: zip file java
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipCompress {
public static void compress(String dirPath) {
final Path sourceDir = Paths.get(dirPath);
String zipFileName = dirPath.concat(".zip");
try {
final ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(zipFileName));
Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
try {
Path targetFile = sourceDir.relativize(file);
outputStream.putNextEntry(new ZipEntry(targetFile.toString()));
byte[] bytes = Files.readAllBytes(file);
outputStream.write(bytes, 0, bytes.length);
outputStream.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
return FileVisitResult.CONTINUE;
}
});
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Example 2: reading zip file in java
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class ZipFileReader {
private static final String FILE_NAME = "C:\\temp\\pics.zip";
private static final String OUTPUT_DIR = "C:\\temp\\Images\\";
private static final int BUFFER_SIZE = 8192;
public static void main(String args[]) throws IOException {
readUsingZipFile();
}
private static void readUsingZipFile() throws IOException {
final ZipFile file = new ZipFile(FILE_NAME);
System.out.println("Iterating over zip file : " + FILE_NAME);
try {
final Enumeration<? extends ZipEntry> entries = file.entries();
while (entries.hasMoreElements()) {
final ZipEntry entry = entries.nextElement();
System.out.printf("File: %s Size %d Modified on %TD %n", entry.getName(), entry.getSize(), new Date(entry.getTime()));
extractEntry(entry, file.getInputStream(entry));
}
System.out.printf("Zip file %s extracted successfully in %s", FILE_NAME, OUTPUT_DIR);
} finally {
file.close();
}
}
private static void readUsingZipInputStream() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(FILE_NAME));
final ZipInputStream is = new ZipInputStream(bis);
try {
ZipEntry entry;
while ((entry = is.getNextEntry()) != null) {
System.out.printf("File: %s Size %d Modified on %TD %n", entry.getName(), entry.getSize(), new Date(entry.getTime()));
extractEntry(entry, is);
}
} finally {
is.close();
}
}
private static void extractEntry(final ZipEntry entry, InputStream is) throws IOException {
String exractedFile = OUTPUT_DIR + entry.getName();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(exractedFile);
final byte[] buf = new byte[BUFFER_SIZE];
int read = 0;
int length;
while ((length = is.read(buf, 0, buf.length)) >= 0) {
fos.write(buf, 0, length);
}
} catch (IOException ioex) {
fos.close();
}
}
}
Output:
Iterating over zip file : C:\temp\pics.zip
File: Image (11).png Size 21294 Modified on 10/24/13
File: Image (1).png Size 22296 Modified on 11/19/13
File: Image (2).png Size 10458 Modified on 10/24/13
File: Image (3).png Size 18425 Modified on 11/19/13
File: Image (4).png Size 31888 Modified on 11/19/13
File: Image (5).png Size 27454 Modified on 11/19/13
File: Image (6).png Size 67608 Modified on 11/19/13
File: Image (7).png Size 8659 Modified on 11/19/13
File: Image (8).png Size 40015 Modified on 11/19/13
File: Image (9).png Size 17062 Modified on 10/24/13
File: Image (10).png Size 42467 Modified on 10/24/13
Zip file C:\temp\pics.zip extracted successfully in C:\temp\Images\