A terrible way of archiving files by turning them into jvm class files
  • Java 87.2%
  • Shell 12.8%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
770grappenmaker 6162c95de9 Write README.md
2026-03-17 23:38:25 +01:00
sillies Initial commit 2026-03-17 22:49:58 +01:00
.gitignore Initial commit 2026-03-17 22:49:58 +01:00
BadParser.java Initial commit 2026-03-17 22:49:58 +01:00
base-archive Write README.md 2026-03-17 23:38:25 +01:00
base-archive.min Add commentless variant 2026-03-17 23:08:43 +01:00
java-archive.sh Add commentless variant 2026-03-17 23:08:43 +01:00
original.png Initial commit 2026-03-17 22:49:58 +01:00
README.md Write README.md 2026-03-17 23:38:25 +01:00
Restore.java Far superior method to what it was before 2026-03-17 23:02:28 +01:00

jvm-class-archive

Proof-of-concept that JVM .class files can be self-extracting archives, where the extraction part of things is done within the JVM.

The program, called Restore.java, reads itself while it is running, parses itself partially, and then prints to standard output the contents of the last Attribute.

Restore.java then gets compiled and disassembled by BadParser.java, which also annotates the raw .class file in hexadecimal representation with some meaning. We cleverly modify it to obtain base-archive, which is essentially a hexadecimal representation of Restore.class, the compiled Restore.java.

Finally, the shell script java-archive.sh can substitute any file of size at most 2^32 bytes into base-archive, which then can be converted back to raw bytes on disk, after which we have successfully prepared a .class file that any JVM that supports class file version 46 (Java 2) can run.

Of course, because Java has had support for GZIP since its earliest versions, this POC can very easily be extended to not just support self-extracting, but also self-decompressing archives.

Archiving an empty file leads to a file size of only 957 bytes, that is how much we need to represent the entire compiled code

Requirements

  • Java 2 or higher
  • Bash
  • xxd

Example

chmod +x java-archive.sh
./java-archive.sh original.png
java original > restored.png
diff -s original.png restored.png
# Files original.png and restored.png are identical

Improvements

As said before, we can make the Java side of things use GZIPInputStream, and then also gzip inputs with gzip in java-archive.sh, in order to have even any benefit from this system at all; without this, we have essentially added a useless header to the original files, which provides barely any steganography. With gzipping, we would have self-extracting compressed folders, which can run on virtually any machine due to its amazing binary compatibility for Java (only Java 1 is unsupported), and Java runs on pretty much any platform anyway. In an environment where it is difficult to install additional tools, (say, lack of packages in the distribution, or no distribution at all), we can usually still get our hands on a distribution of Java.

Additionally, we could add cryptography, which would make for self-decoding archives.

Of course, these improvements and applicability concerns listed above are entirely ridiculous.

Limitations

  • Files can be of size at most 2^32 bytes
  • File names can be of length at most 2^16 - 6 characters
  • Java 1 incompatible
  • Renaming archives breaks them, the java command wants the name of the class and of the file to match