-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
554c4ee
commit 34cde11
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
||
|
||
} |