

GitHub - helpermethod/zip-forge: A tiny, formatter-friendly Java DSL for creatin...
source link: https://github.com/helpermethod/zip-forge
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

🌋 ZIP Forge
A tiny, formatter-friendly Java DSL for creating ZIP files.
✨ Features
🤏 Tiny
The ZIP Forge API consists of only 3 methods.
📋 Formatter-friendly
Contrary to other DSLs, applying a code formatter like palantir-java-format will not mess up ZIP Forge's indentation.
📦 No external dependencies
ZIP Forge is based on Java's ZIP File System Provider and requires no external dependencies.
🧩 Modular
ZIP Forge is published as a Java 9 module but still compatible with Java 8.
🛠️ Installation
Maven
<dependency>
<groupId>io.github.helpermethod</groupId>
<artifactId>zip-forge</artifactId>
<version>1.0.0</version>
</dependency>
Gradle
implementation 'io.github.helpermethod:zip-forge:1.0.0'
Gradle (Kotlin)
implementation("io.github.helpermethod:zip-forge:1.0.0")
🔍 Usage
The following code snippet calls createZipFile
to create the ZIP file at the given location.
It uses the file
and directory
methods to create files and directories within the context of the ZIP file.
⚠️file
and directory
should never be used outside of createZipFile
's or directory
's context.
import java.nio.charset.StandardCharsets;
import static io.github.helpermethod.zipforge.ZipForge.createZipFile;
import static io.github.helpermethod.zipforge.ZipForge.file;
import static io.github.helpermethod.zipforge.ZipForge.directory;
import static java.nio.charset.StandardCharsets.UTF_8;
class ZipForgeDemo {
public static void main(String[] args) throws IOException {
// creates a ZIP file named demo.zip in the /home/helpermethod directory
createZipFile(Paths.get("/home/helpermethod/demo.zip"), () -> {
// the file content can be specified as a String...
file("a.txt", "a");
directory("d", () -> {
// ... or a byte[]...
file("b.txt", "b".getBytes(UTF_8));
// ... or a Path
file("c.bin", Paths.get("c.bin"));
// directories can be nested
directory("e", () -> {
file("f.txt", "f");
});
});
});
}
}
The above code results in a ZIP file with the following structure.
Archive: demo.zip
Length Date Time Name
--------- ---------- ----- ----
0 07-11-2023 15:39 d/
0 07-11-2023 15:39 d/e/
1 07-11-2023 15:39 a.txt
1 07-11-2023 15:39 d/b.txt
1 07-11-2023 15:39 d/c.bin
1 07-11-2023 15:39 d/e/f.txt
--------- -------
4 6 files
Kotlin
The same example written in Kotlin. Note that it uses the same API as the Java version.
import io.github.helpermethod.zipforge.ZipForge.createZipFile
import io.github.helpermethod.zipforge.ZipForge.directory
import io.github.helpermethod.zipforge.ZipForge.file
import kotlin.io.path.Path
fun main() {
createZipFile(Path("/home/helpermethod/demo.zip")) {
file("a.txt", "a")
directory("d") {
file("b.txt", "b".toByteArray())
file("c.bin", Path("c.bin"))
directory("e") {
file("f.txt", "f")
}
}
}
}
Recommend
-
170
-
181
vim-mix-format Elixir 1.6 introduced the formatter: mix format. This plugin makes it easy to run the formatter asynchronously from within Vim 8 and Neovim. Installation Use your
-
146
-
120
Introduction Most of the current formatters for Python --- e.g., autopep8, and pep8ify --- are made to remove lint errors from code. This has some obvious limitations. For i...
-
84
cmake_format - Source code formatter for cmake listfiles.
-
127
README.md black
-
355
README.md sh
-
72
README.md sqlfmt Experimental SQL formatter with width-aware output Based on
-
12
ECS Liva Q1D Mini-PC Review: The Most Eco-Friendly Tiny PC Ever By Kannon Yamada Published Mar 18, 2021...
-
10
.NET Multi-platform App UI (.NET MAUI) is a cross-platform framework for creating native mobile and desktop apps Using .NET MAUI to Build a Mobile App...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK