BAEL-6804: Difference Between ZipFile and ZipInputStream in Java (#15039)

* BAEL-6804: Difference Between ZipFile and ZipInputStream in Java

* BAEL-6804: Difference Between ZipFile and ZipInputStream in Java
- use 2-space indents for line continuations
This commit is contained in:
Manfred
2023-10-30 00:56:52 +00:00
committed by GitHub
parent b7edd335a9
commit a127e7644e
4 changed files with 281 additions and 0 deletions
@@ -0,0 +1,112 @@
package com.baeldung.zip;
import java.io.*;
import java.util.concurrent.TimeUnit;
import java.util.*;
import java.util.zip.*;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
@State(Scope.Benchmark)
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 1, time = 2, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 3, time = 1, timeUnit = TimeUnit.SECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Fork(value = 1)
public class ZipBenchmark {
public static final int NUM_OF_FILES = 10;
public static final int DATA_SIZE = 204800;
@State(Scope.Thread)
public static class SourceState {
public File compressedFile;
@Setup(Level.Trial)
public void setup() throws IOException {
ZipSampleFileStore sampleFileStore = new ZipSampleFileStore(NUM_OF_FILES, DATA_SIZE);
compressedFile = sampleFileStore.getFile();
}
@TearDown(Level.Trial)
public void cleanup() {
if (compressedFile.exists()) {
compressedFile.delete();
}
}
}
@Benchmark
public static void readAllEntriesByZipFile(SourceState sourceState, Blackhole blackhole) throws IOException {
try (ZipFile zipFile = new ZipFile(sourceState.compressedFile)) {
Enumeration<? extends ZipEntry> zipEntries = zipFile.entries();
while (zipEntries.hasMoreElements()) {
ZipEntry zipEntry = zipEntries.nextElement();
try (InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry))) {
blackhole.consume(ZipSampleFileStore.getString(inputStream));
}
}
}
}
@Benchmark
public static void readAllEntriesByZipInputStream(SourceState sourceState, Blackhole blackhole) throws IOException {
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceState.compressedFile));
ZipInputStream zipInputStream = new ZipInputStream(bis)
) {
ZipEntry entry;
while ((entry = zipInputStream.getNextEntry()) != null) {
blackhole.consume(ZipSampleFileStore.getString(zipInputStream));
}
}
}
@Benchmark
public static void readLastEntryByZipFile(SourceState sourceState, Blackhole blackhole) throws IOException {
try (ZipFile zipFile = new ZipFile(sourceState.compressedFile)) {
ZipEntry zipEntry = zipFile.getEntry(getLastEntryName());
try (InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry))) {
blackhole.consume(ZipSampleFileStore.getString(inputStream));
}
}
}
@Benchmark
public static void readLastEntryByZipInputStream(SourceState sourceState, Blackhole blackhole) throws IOException {
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(sourceState.compressedFile));
ZipInputStream zipInputStream = new ZipInputStream(bis)
) {
ZipEntry entry;
while ((entry = zipInputStream.getNextEntry()) != null) {
if (Objects.equals(entry.getName(), getLastEntryName())){
blackhole.consume(ZipSampleFileStore.getString(zipInputStream));
}
}
}
}
private static String getLastEntryName() {
return String.format(ZipSampleFileStore.ENTRY_NAME_PATTERN, NUM_OF_FILES);
}
public static void main(String[] args) throws Exception {
Options options = new OptionsBuilder()
.include(ZipBenchmark.class.getSimpleName()).threads(1)
.shouldFailOnError(true)
.shouldDoGC(true)
.jvmArgs("-server").build();
new Runner(options).run();
}
}
@@ -0,0 +1,70 @@
package com.baeldung.zip;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.commons.io.IOUtils;
public class ZipSampleFileStore {
public static final String ENTRY_NAME_PATTERN = "str-data-%s.txt";
private static final Charset DEFAULT_ENCODING = StandardCharsets.UTF_8;
private final File file;
private final List<String> dataList;
public ZipSampleFileStore(int numOfFiles, int fileSize) throws IOException {
dataList = new ArrayList<>(numOfFiles);
file = File.createTempFile("zip-sample", "");
try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file))) {
for (int idx=0; idx<=numOfFiles; idx++) {
byte[] data = createRandomStringInByte(fileSize);
dataList.add(new String(data, DEFAULT_ENCODING));
ZipEntry entry = new ZipEntry(String.format(ENTRY_NAME_PATTERN, idx));
zos.putNextEntry(entry);
zos.write(data);
zos.closeEntry();
}
}
}
public static byte[] createRandomStringInByte(int size) {
Random random = new Random();
byte[] data = new byte[size];
for (int n = 0; n < data.length; n++) {
char randomChar;
int choice = random.nextInt(2); // 0 for uppercase, 1 for lowercase
if (choice == 0) {
randomChar = (char) ('A' + random.nextInt(26)); // 'A' to 'Z'
} else {
randomChar = (char) ('a' + random.nextInt(26)); // 'a' to 'z'
}
data[n] = (byte) randomChar;
}
return data;
}
public File getFile() {
return file;
}
public List<String> getDataList() {
return dataList;
}
public static String getString(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
IOUtils.copy(inputStream, byteArrayOutputStream);
return byteArrayOutputStream.toString(DEFAULT_ENCODING);
}
}