-
Notifications
You must be signed in to change notification settings - Fork 3
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
96c4f7c
commit 2703d96
Showing
3 changed files
with
84 additions
and
2 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
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,49 @@ | ||
package io.seqera.util; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.zip.DeflaterInputStream; | ||
import java.util.zip.InflaterInputStream; | ||
|
||
/** | ||
* Helper class to basic compress/decompress funcionality | ||
* | ||
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com> | ||
*/ | ||
public class ZipUtils { | ||
|
||
public static byte[] compress(InputStream stream) { | ||
try (stream) { | ||
return new DeflaterInputStream(stream).readAllBytes(); | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException("Unable to compress provider stream", e); | ||
} | ||
} | ||
|
||
public static byte[] compress(byte[] bytes) { | ||
return compress(new ByteArrayInputStream(bytes)); | ||
} | ||
|
||
public static byte[] compress(String text) throws IOException { | ||
return compress(new ByteArrayInputStream(text.getBytes())); | ||
} | ||
|
||
public static InputStream decompress(byte[] buffer) { | ||
return new InflaterInputStream(new ByteArrayInputStream(buffer)); | ||
} | ||
|
||
public static String decompressAsString(byte[] buffer) throws IOException { | ||
return new String(decompress(buffer).readAllBytes()); | ||
} | ||
|
||
public static byte[] decompressAsBytes(byte[] buffer) { | ||
try { | ||
return decompress(buffer).readAllBytes(); | ||
} | ||
catch (IOException e) { | ||
throw new RuntimeException("Unable to decompress provider buffer", e); | ||
} | ||
} | ||
} |
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,31 @@ | ||
package io.seqera.util | ||
|
||
import spock.lang.Specification | ||
|
||
/** | ||
* | ||
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com> | ||
*/ | ||
class ZipUtilsTest extends Specification { | ||
|
||
def 'should compress/decompress a text'() { | ||
given: | ||
def TEXT = 'Hello world\n' * 10 | ||
|
||
when: | ||
def buffer = ZipUtils.compress(TEXT) | ||
then: | ||
buffer.size() > 0 && buffer.size() < TEXT.bytes.size() | ||
|
||
when: | ||
def copy = ZipUtils.decompressAsString(buffer) | ||
then: | ||
copy == TEXT | ||
|
||
when: | ||
def bytes = ZipUtils.decompressAsBytes(buffer) | ||
then: | ||
bytes == TEXT.bytes | ||
} | ||
|
||
} |