This PR is related to BAEL-7427 (#16028)

* This commit is related to BAEL-7427

This commit aims to add a Main class.

* This commit is related to BAEL-7427

This commit aims to add a myFile.gz file to the resources folder.

* This commit is related to BAEL-7427

This commit aims to add a test class "ReadingGZIPUsingGZIPInputStreamUnitTest".

* This commit is related to BAEL-7427

This commit aims to add a myFile.gz file to the resources folder.

* Update Main.java

* Update ReadingGZIPUsingGZIPInputStreamUnitTest.java

* Update Main.java

* Update ReadingGZIPUsingGZIPInputStreamUnitTest.java

* Update ReadingGZIPUsingGZIPInputStreamUnitTest.java

* Update Main.java

* Update Main.java

* Update ReadingGZIPUsingGZIPInputStreamUnitTest.java

* Update Main.java

* Update ReadingGZIPUsingGZIPInputStreamUnitTest.java

* Update Main.java

* Update ReadingGZIPUsingGZIPInputStreamUnitTest.java

* Update Main.java
This commit is contained in:
Mo Helmy 2024-03-24 22:08:17 +02:00 committed by GitHub
parent ff12386947
commit 17cb4108d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 123 additions and 0 deletions

View File

@ -0,0 +1,73 @@
package com.baeldung.usinggzipInputstream;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;
import static java.util.stream.Collectors.toList;
public class Main {
static String filePath = Objects.requireNonNull(Main.class.getClassLoader().getResource("myFile.gz")).getFile();
public static void main(String[] args) throws IOException {
// Test readGZipFile method
List<String> fileContents = readGZipFile(filePath);
System.out.println("Contents of GZIP file:");
fileContents.forEach(System.out::println);
// Test findInZipFile method
String searchTerm = "Line 1 content";
List<String> foundLines = findInZipFile(filePath, searchTerm);
System.out.println("Lines containing '" + searchTerm + "' in GZIP file:");
foundLines.forEach(System.out::println);
// Test useContentsOfZipFile method
System.out.println("Using contents of GZIP file with consumer:");
useContentsOfZipFile(filePath, linesStream -> {
linesStream.filter(line -> line.length() > 10).forEach(System.out::println);
});
}
public static List<String> readGZipFile(String filePath) throws IOException {
List<String> lines = new ArrayList<>();
try (InputStream inputStream = new FileInputStream(filePath);
GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
InputStreamReader inputStreamReader = new InputStreamReader(gzipInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
String line;
while ((line = bufferedReader.readLine()) != null) {
lines.add(line);
}
}
return lines;
}
public static List<String> findInZipFile(String filePath, String toFind) throws IOException {
try (InputStream inputStream = new FileInputStream(filePath);
GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
InputStreamReader inputStreamReader = new InputStreamReader(gzipInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
return bufferedReader.lines().filter(line -> line.contains(toFind)).collect(toList());
}
}
public static void useContentsOfZipFile(String filePath, Consumer<Stream<String>> consumer) throws IOException {
try (InputStream inputStream = new FileInputStream(filePath);
GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream);
InputStreamReader inputStreamReader = new InputStreamReader(gzipInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
consumer.accept(bufferedReader.lines());
}
}
}

View File

@ -0,0 +1,50 @@
package com.baeldung.usinggzipInputstream;
import org.junit.jupiter.api.Test;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static org.junit.Assert.assertEquals;
public class ReadingGZIPUsingGZIPInputStreamUnitTest {
String testFilePath = Objects.requireNonNull(ReadingGZIPUsingGZIPInputStreamUnitTest.class.getClassLoader().getResource("myFile.gz")).getFile();
List<String> expectedFilteredLines = Arrays.asList("Line 1 content", "Line 2 content", "Line 3 content");
@Test
void givenGZFile_whenUsingGZIPInputStream_thenReadLines() throws IOException {
try (Stream<String> lines = Main.readGZipFile(testFilePath).stream()) {
List<String> result = lines
.filter(expectedFilteredLines::contains)
.collect(Collectors.toList());
assertEquals(expectedFilteredLines, result);
}
}
@Test
void givenGZFile_whenUsingtestFindInZipFile_thenReadLines() throws IOException {
String toFind = "Line 1 content";
List<String> result = Main.findInZipFile(testFilePath, toFind);
assertEquals("Line 1 content", result.get(0));
}
@Test
void givenGZFile_whenUsingContentsOfZipFile_thenReadLines() throws IOException {
AtomicInteger count = new AtomicInteger(0);
Main.useContentsOfZipFile(testFilePath, linesStream -> {
linesStream.filter(line -> line.length() > 10).forEach(line -> count.incrementAndGet());
});
assertEquals(3, count.get());
}
}