Skip to content

Instantly share code, notes, and snippets.

@nsdevaraj
Created May 19, 2010 07:29
Show Gist options
  • Save nsdevaraj/406059 to your computer and use it in GitHub Desktop.
Save nsdevaraj/406059 to your computer and use it in GitHub Desktop.
package com.westberg.swfcompress;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterInputStream;
import java.util.zip.InflaterInputStream;
public class SWFCompress
{
/**
* @param args
*/
public static void main(String[] args)
{
if(args.length < 2)
{
usage();
}
if(args[0].equalsIgnoreCase("c"))
{
//compress
try
{
DataInputStream dis = new DataInputStream(new FileInputStream(args[1]));
byte[] header = new byte[8];
dis.read(header);
if(header[0] != 0x46) //'F'
{
System.out.println("Un-Compressed header not found. cannot compress");
}
else
{
//yay, we have an un-compressed file. compress it and write it out
DataOutputStream dos = new DataOutputStream(new FileOutputStream("c_"+args[1]));
header[0] = 0x43; //'F';
dos.write(header);
byte[] chunk = new byte[2048];
int bytesRead = 0;
DeflaterInputStream compressor = new DeflaterInputStream(dis);
while((bytesRead = compressor.read(chunk)) > -1)
{
dos.write(chunk, 0, bytesRead);
}
dos.close();
System.out.println("Compressed to file: c_" +args[1]);
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
else if(args[0].equalsIgnoreCase("d"))
{
//decompress
try
{
DataInputStream dis = new DataInputStream(new FileInputStream(args[1]));
byte[] header = new byte[8];
dis.read(header);
if(header[0] != 0x43) //'C'
{
System.out.println("Compressed header not found. cannot decompress");
}
else
{
//yay, we have a compressed file. decompress it and write it out
DataOutputStream dos = new DataOutputStream(new FileOutputStream("d_"+args[1]));
header[0] = 0x46; //'F';
dos.write(header);
byte[] chunk = new byte[2048];
int bytesRead = 0;
InflaterInputStream decompressor = new InflaterInputStream(dis);
while((bytesRead = decompressor.read(chunk)) > -1)
{
dos.write(chunk, 0, bytesRead);
}
dos.close();
System.out.println("Decompressed to file: d_" +args[1]);
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
else
{
usage();
}
}
private static void usage()
{
System.out.println("Usage: java -jar SWFCompress.jar [c|d] Movie.swf");
System.out.println("\tc - compress a swf file");
System.out.println("\td - decompress a swf file");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment