I'm not really a fan of GUI's since I might write code to do something once and then never use it again so I write the most simplistic thing to get the job done, and if I need anything changed I just edit the code. :p So here's my interpretation of a simple file packer using GZIP compression.
Packer
Spoiler (Click to View)
Unpacker
Spoiler (Click to View)
Packer
Spoiler (Click to View)
Code
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.GZIPOutputStream;
import javax.swing.filechooser.FileSystemView;
public class PackGZip {
private static final File packDir, fileCache;
static {
packDir = new File(FileSystemView.getFileSystemView().getHomeDirectory(), "HF"); //this is the directory of the files it will compress
fileCache = new File(packDir, "howbow.dat"); //this is the file it will compress to
}
public static void compressContents() {
if(!packDir.exists()) {
System.out.println("Packing directory does not exist!");
return;
}
File[] datFiles = packDir.listFiles(f -> {
//write your own custom file filter if you'd like
return f.isFile();
});
try (GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(fileCache)); DataOutputStream dataFile = new DataOutputStream(gzos)) {
dataFile.writeInt(datFiles.length);
System.out.println("Appending " + datFiles.length + " entries to config");
for (File f : datFiles) {
int length = (int) f.length();
dataFile.writeInt(length);
String name = f.getName();
dataFile.writeUTF(name);
System.out.println(String.format("Appending to config bytes=%d name=%s", length, name));
byte[] data = readFile(f);
dataFile.write(data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private static byte[] readFile(File file) {
try {
Path path = Paths.get(file.getAbsolutePath());
byte[] data = Files.readAllBytes(path);
return data;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
compressContents();
}
}Unpacker
Spoiler (Click to View)
Code
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import javax.swing.filechooser.FileSystemView;
public class UnpackGZip {
private static final File unpackDir, fileCache;
static {
unpackDir = new File(FileSystemView.getFileSystemView().getHomeDirectory(), "HF"); //this is the folder the compressed file will be extracted to
fileCache = new File(unpackDir, "howbow.dat"); //this is the compressed file
}
public static void decompressContents() {
if(!unpackDir.exists() || !fileCache.exists()) {
System.out.println("Your unpacking directory or file cache does not exist!");
return;
}
try (GZIPInputStream gzos = new GZIPInputStream(new FileInputStream(fileCache)); DataInputStream dataFile = new DataInputStream(gzos)) {
int length = dataFile.readInt();
for (int i = 0; i < length; i++) {
int buffer = dataFile.readInt();
String name = dataFile.readUTF();
byte[] data = new byte[buffer];
dataFile.readFully(data);
saveToFile(name, data);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void saveToFile(String name, byte[] data) {
try (FileOutputStream fos = new FileOutputStream(new File(unpackDir, name))) {
fos.write(data);
System.out.println("Saved: " + name);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
decompressContents();
}
}