Created
January 16, 2019 20:43
-
-
Save mcenderdragon/29cf204c267e65f2981d9e35ae9f110b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public static void main(String[] args) | |
{ | |
byte[] b = preCompress(64); | |
byte[] a; | |
while(true) | |
{ | |
try | |
{ | |
FileOutputStream out = new FileOutputStream(new File("./" + b.length +".gz")); | |
out.write(b); | |
out.close(); | |
} | |
catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
System.out.println(b.length); | |
a = compress(b); | |
if(a.length < b.length) | |
{ | |
b = a; | |
} | |
else | |
{ | |
break; | |
} | |
} | |
System.out.println("Done"); | |
System.out.println(b.length); | |
System.out.println(a.length); | |
System.out.println(Arrays.toString(b)); | |
System.out.println(new String(b)); | |
try | |
{ | |
FileOutputStream out = new FileOutputStream(new File("./zip.gz")); | |
out.write(b); | |
out.close(); | |
} | |
catch (FileNotFoundException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static byte[] compress(byte[] b) | |
{ | |
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
try { | |
GZIPOutputStream gzip = new GZIPOutputStream(out); | |
gzip.write(b); | |
gzip.close(); | |
out.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return out.toByteArray(); | |
} | |
private static byte[] preCompress(final int max) | |
{ | |
System.out.println("Creating..."); | |
byte[] b = new byte[1024 * 1024 * 1024]; | |
Arrays.fill(b, (byte) 1); | |
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
try { | |
GZIPOutputStream gzip = new GZIPOutputStream(out); | |
for(int i=0;i<max;i++) | |
{ | |
System.out.printf("%s/%s.", i, max); | |
gzip.write(b); | |
System.out.print("."); | |
gzip.flush(); | |
System.out.print("."); | |
} | |
gzip.close(); | |
out.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return out.toByteArray(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dynamic generation of multiple compressed gzip files with variable size.