Path: blob/main/epkcompiler/src/CompilePackage.java
8641 views
import java.io.ByteArrayOutputStream;1import java.io.File;2import java.io.FileInputStream;3import java.io.FileOutputStream;4import java.io.IOException;5import java.io.InputStream;6import java.io.OutputStream;7import java.nio.charset.Charset;8import java.nio.charset.StandardCharsets;9import java.text.SimpleDateFormat;10import java.util.ArrayList;11import java.util.Date;1213import com.jcraft.jzlib.CRC32;14import com.jcraft.jzlib.Deflater;15import com.jcraft.jzlib.DeflaterOutputStream;16import com.jcraft.jzlib.GZIPOutputStream;1718public class CompilePackage {1920private static ArrayList<File> files = new ArrayList();2122public static void main(String[] args) throws IOException {23if(args.length < 2 || args.length > 4) {24System.out.println("Usage: java -jar CompilePackage.jar <input directory> <output file> [gzip|zlib|none] [file-type]");25return;26}2728File root = new File(args[0]);29File output = new File(args[1]);30char compressionType;3132if(args.length > 2) {33if(args[2].equalsIgnoreCase("gzip")) {34compressionType = 'G';35}else if(args[2].equalsIgnoreCase("zlib")) {36compressionType = 'Z';37}else if(args[2].equalsIgnoreCase("none")) {38compressionType = '0';39}else {40throw new IllegalArgumentException("Unknown compression method: " + args[2]);41}42}else {43compressionType = 'G';44}4546listDirectory(root);47ByteArrayOutputStream osb = new ByteArrayOutputStream();48String start = root.getAbsolutePath();4950osb.write("EAGPKG$$".getBytes(Charset.forName("UTF-8")));5152String chars = "ver2.0";53osb.write(chars.length());54osb.write(chars.getBytes(StandardCharsets.US_ASCII));5556Date d = new Date();5758String comment = "\n\n # Eagler EPK v2.0 (c) " + (new SimpleDateFormat("yyyy")).format(d) + " Calder Young\n" +59" # update: on " + (new SimpleDateFormat("MM/dd/yyyy")).format(d) + " at " +60(new SimpleDateFormat("hh:mm:ss aa")).format(d) + "\n\n";6162String nm = output.getName();63osb.write(nm.length());64osb.write(nm.getBytes(StandardCharsets.US_ASCII));6566writeShort(comment.length(), osb);67osb.write(comment.getBytes(StandardCharsets.US_ASCII));6869writeLong(d.getTime(), osb);70writeInt(files.size() + 1, osb);7172osb.write(compressionType);7374OutputStream os;7576if(compressionType == 'G') {77os = new GZIPOutputStream(osb, new Deflater(9, 15+16), 16384, true);78}else if(compressionType == 'Z') {79os = new DeflaterOutputStream(osb, new Deflater(9), 16384, true);80}else {81os = osb;82}8384os.write("HEAD".getBytes(StandardCharsets.US_ASCII));85String key = "file-type";86os.write(key.length());87os.write(key.getBytes(StandardCharsets.US_ASCII));88String value;89if(args.length > 3) {90value = args[3];91}else {92value = "epk/resources";93}94writeInt(value.length(), os);95os.write(value.getBytes(StandardCharsets.US_ASCII));96os.write('>');9798CRC32 checkSum = new CRC32();99for(File f : files) {100InputStream stream = new FileInputStream(f);101byte[] targetArray = new byte[(int)f.length()];102stream.read(targetArray);103stream.close();104105checkSum.reset();106checkSum.update(targetArray, 0, targetArray.length);107int ch = (int)checkSum.getValue();108109os.write("FILE".getBytes(StandardCharsets.US_ASCII));110111String p = f.getAbsolutePath().replace(start, "").replace('\\', '/');112if(p.startsWith("/")) {113p = p.substring(1);114}115os.write(p.length());116os.write(p.getBytes(StandardCharsets.US_ASCII));117writeInt(targetArray.length + 5, os);118writeInt(ch, os);119120os.write(targetArray);121os.write(':');122os.write('>');123}124125os.write("END$".getBytes(StandardCharsets.US_ASCII));126os.close();127128osb.write(":::YEE:>".getBytes(StandardCharsets.US_ASCII));129130FileOutputStream out = new FileOutputStream(output);131out.write(osb.toByteArray());132out.close();133}134135public static void writeShort(int i, OutputStream os) throws IOException {136os.write((i >> 8) & 0xFF);137os.write(i & 0xFF);138}139140public static void writeInt(int i, OutputStream os) throws IOException {141os.write((i >> 24) & 0xFF);142os.write((i >> 16) & 0xFF);143os.write((i >> 8) & 0xFF);144os.write(i & 0xFF);145}146147public static void writeLong(long i, OutputStream os) throws IOException {148os.write((int)((i >> 56) & 0xFF));149os.write((int)((i >> 48) & 0xFF));150os.write((int)((i >> 40) & 0xFF));151os.write((int)((i >> 32) & 0xFF));152os.write((int)((i >> 24) & 0xFF));153os.write((int)((i >> 16) & 0xFF));154os.write((int)((i >> 8) & 0xFF));155os.write((int)(i & 0xFF));156}157158public static void listDirectory(File dir) {159for(File f : dir.listFiles()) {160if(f.isDirectory()) {161listDirectory(f);162}else {163files.add(f);164}165}166}167168}169170171