From 34cde11ddc7906ea4f3662c6b7d23155aaf8e961 Mon Sep 17 00:00:00 2001 From: Ben Hesford Date: Mon, 18 Nov 2024 12:29:22 -0700 Subject: [PATCH] WIP --- .../java/gov/cms/ab2d/common/GzipTesting.java | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 common/src/main/java/gov/cms/ab2d/common/GzipTesting.java diff --git a/common/src/main/java/gov/cms/ab2d/common/GzipTesting.java b/common/src/main/java/gov/cms/ab2d/common/GzipTesting.java new file mode 100644 index 000000000..9e0cf86fd --- /dev/null +++ b/common/src/main/java/gov/cms/ab2d/common/GzipTesting.java @@ -0,0 +1,73 @@ +package gov.cms.ab2d.common; + +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.zip.GZIPInputStream; +import java.util.zip.GZIPOutputStream; + +public class GzipTesting { + + static final int BUFFER_SIZE = 1024 * 4; + + public static void main(String[] args) { + String originalFile = "/Users/benhesford/Downloads/ndjson-example-files/bigfile-copy.ndjson"; + String gzippedFile = "/Users/benhesford/Downloads/ndjson-example-files/bigfile.ndjson.gz"; + + decompressGzipFile(gzippedFile, "/tmp/bigfile-decompressed-java.ndjson"); + + compressGzipFile(originalFile, "/tmp/bigfile-compressed-java.gz"); + + + } + + private static void decompressGzipFile(String gzipFile, String newFile) { + long now = System.currentTimeMillis(); + try { + FileInputStream fis = new FileInputStream(gzipFile); + GZIPInputStream gis = new GZIPInputStream(fis); + FileOutputStream fos = new FileOutputStream(newFile); + byte[] buffer = new byte[BUFFER_SIZE]; + int len; + while((len = gis.read(buffer)) != -1){ + fos.write(buffer, 0, len); + } + //close resources + fos.close(); + gis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + long completed = System.currentTimeMillis(); + System.out.println((completed-now)/1000.0 + " seconds to decompress"); + } + + private static void compressGzipFile(String file, String gzipFile) { + long now = System.currentTimeMillis(); + + try { + FileInputStream fis = new FileInputStream(file); + FileOutputStream fos = new FileOutputStream(gzipFile); + GZIPOutputStream gzipOS = new GZIPOutputStream(fos) { + { + //def.setLevel(1); + } + }; + byte[] buffer = new byte[BUFFER_SIZE]; + int len; + while((len=fis.read(buffer)) != -1){ + gzipOS.write(buffer, 0, len); + } + //close resources + gzipOS.close(); + fos.close(); + fis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + long completed = System.currentTimeMillis(); + System.out.println((completed-now)/1000.0 + " seconds to compress"); + } + + +}