Skip to content

Instantly share code, notes, and snippets.

@mcenderdragon
Created January 16, 2019 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcenderdragon/29cf204c267e65f2981d9e35ae9f110b to your computer and use it in GitHub Desktop.
Save mcenderdragon/29cf204c267e65f2981d9e35ae9f110b to your computer and use it in GitHub Desktop.
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();
}
@mcenderdragon
Copy link
Author

Dynamic generation of multiple compressed gzip files with variable size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment