Skip to content

Commit

Permalink
feat(Writer): aligned entries
Browse files Browse the repository at this point in the history
  • Loading branch information
rushiiMachine committed May 1, 2022
1 parent 48e819d commit fdb94c9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,21 @@ class MainActivity : AppCompatActivity() {

ZipWriter(zipFile, true).use { zip ->
zip.setComment("a comment".toByteArray())
zip.writeEntry("test.txt", "hot garbage")
zip.writeEntryUncompressed("uncompressed.txt", "ihy".toByteArray())
zip.deleteEntries("abc.txt")

val text = "hot garbage".toByteArray()
zip.writeEntry("compressed.txt", text)
zip.writeEntryUncompressed("uncompressed.txt", text)
zip.writeAligned("aligned.txt", 4, text)
zip.writeUncompressedAligned("uncompressedaligned.txt", 4, text)
}

ZipReader(zipFile).use { zip ->
Log.i(TAG, "Modified zip comment: ${zip.comment}")
Log.i(TAG, "Modified zip entries: ${zip.entryNames.joinToString()}")
Log.i(TAG, "Created entry content: ${zip.openEntry("test.txt")?.read()?.decodeToString()}")

val content = zip.openEntry("compressed.txt")?.read()?.decodeToString()
Log.i(TAG, "Created entry content: $content")
}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = "com.github.diamondminer88"
version = "1.0.0"
version = "1.1.0"

plugins {
id("com.android.library")
Expand Down
42 changes: 42 additions & 0 deletions lib/rust/src/zip_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use jni::{
objects::{JClass, JString},
sys::{jboolean, jbyteArray, jobjectArray, jsize},
};
use jni::sys::jshort;
use jni_fn::jni_fn;
use zip::{CompressionMethod, ZipArchive, ZipWriter};
use zip::write::FileOptions;
Expand Down Expand Up @@ -134,6 +135,47 @@ pub fn writeEntryUncompressed(
writer.write_all(&bytes).unwrap();
}

#[jni_fn("com.github.diamondminer88.zip.ZipWriter")]
pub fn writeUncompressedAligned(
env: JNIEnv,
class: JClass,
path: JString,
alignment: jshort,
bytes: jbyteArray,
) {
let path = env.get_string(path).unwrap();
let alignment = alignment as u16;
let bytes = env.convert_byte_array(bytes).unwrap();
let mut writer = get_writer(&env, class);

let options = FileOptions::default()
.compression_method(CompressionMethod::Stored);

writer.start_file_aligned(path, options, alignment).unwrap();
writer.write_all(&bytes).unwrap();
}

#[jni_fn("com.github.diamondminer88.zip.ZipWriter")]
pub fn writeAligned(
env: JNIEnv,
class: JClass,
path: JString,
alignment: jshort,
bytes: jbyteArray,
) {
let path = env.get_string(path).unwrap();
let alignment = alignment as u16;
let bytes = env.convert_byte_array(bytes).unwrap();
let mut writer = get_writer(&env, class);

writer.start_file_aligned(
path,
FileOptions::default(),
alignment,
).unwrap();
writer.write_all(&bytes).unwrap();
}

#[jni_fn("com.github.diamondminer88.zip.ZipWriter")]
pub fn writeDir(
env: JNIEnv,
Expand Down
20 changes: 18 additions & 2 deletions lib/src/main/java/com/github/diamondminer88/zip/ZipWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public ZipWriter(File file, boolean append) {
/**
* Create an entry and write bytes to it.
* @param path Path to entry inside the archive
* @param data Raw bytes
* @param data Raw data
*/
public native void writeEntry(String path, byte[] data);

Expand All @@ -100,10 +100,26 @@ public void writeEntry(String path, String content) {
/**
* Create an entry and write bytes to it without compressing.
* @param path Path to entry inside the archive
* @param data Raw bytes
* @param data Raw data
*/
public native void writeEntryUncompressed(String path, byte[] data);

/**
* Create an aligned entry and write bytes to it without compressing.
* @param path Path to entry inside the archive
* @param alignment Byte alignment
* @param data Raw data
*/
public native void writeUncompressedAligned(String path, short alignment, byte[] data);

/**
* Create an aligned entry and write bytes to it.
* @param path Path to entry inside the archive
* @param alignment Byte alignment
* @param data Raw data
*/
public native void writeAligned(String path, short alignment, byte[] data);

/**
* Create a directory in the archive.
* @param path Path to directory. Will automatically append a `/` if the path does not end with one already.
Expand Down

0 comments on commit fdb94c9

Please sign in to comment.