Giter Site home page Giter Site logo

zipalign-java's Introduction

zipalign-java

Zipalign implemented in java without any dependencies. Read my post about it :>

Using

Pass in a RandomAccessFile to the function ZipAlign#alignZip to provide ZipAlign with a seekable file for more performant aligning.

import com.iyxan23.zipalignjava.ZipAlign;

RandomAccessFile zipIn = new RandomAccessFile(zipPath, "r");
FileOutputStream zipOut = ...;

ZipAlign.alignZip(zipIn, zipOut);

Additionaly, you could use a regular InputStream.

import com.iyxan23.zipalignjava.ZipAlign;

FileInputStream zipIn = ...;
FileOutputStream zipOut = ...;

ZipAlign.alignZip(zipIn, zipOut);

Aligning .so files to 4KiB page boundaries are enabled by default, pass in a boolean to opt out:

ZipAlign.alignZip(zipIn, zipOut, false);

Importing

This library is published in jitpack, you can add it as your dependency it with:

allprojects {
    repositories {
        mavenCentral()
        maven { url "https://jitpack.io" }
    }
}

dependencies {
    implementation 'com.github.iyxan23:zipalign-java:1.1.3'
}

You could also use prebuilt jars by the CI or in the releases section.

The jar contains a simple CLI that allows you to use it in the command line.

$ java -jar zipalign-java-1.1.3.jar input.zip output.zip

Benchmarks

Benchmarks are ran on an AMD Ryzen 5-5500U CPU with IntelliJ IDEA's runner and Temurin JDK 18.

Function                                             Avg elapsed time   Input

ZipAlign#alignZip(RandomAccessFile, OutputStream)    28ms               File from https://github.com/Iyxan23/zipalign-java/issues/1#issue-1236875761 (270K)
ZipAlign#alignZip(InputStream, OutputStream)         497ms              -- same file --

zipalign-java's People

Contributors

dependabot[bot] avatar iyxan23 avatar klecko avatar pranavpurwar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

zipalign-java's Issues

page-align uncompressed .so files

Thanks for providing a Java implementation of zipalign.

From my perspective it looks quite good, it only misses the possibilities to align .so files by 4096 instead of default 4.

This options is becoming more and more important as recent Android Gradle plugin versions automatically set extractNativeLibs=false which means they are no longer extracted from the APK file upon installation, but directly used from within the APK.
For doing so it is required to store the .so files uncompressed and align them to page boundaries (4096), independent of the value the other files are aligned to.

Wrong file is produced when align is not needed

Error is in the code here:

        file.seek(0);
        if (neededAlignments.size() == 0) {
            // there is no needed alignment, stream it all!
            byte[] buffer = new byte[8192];
            while (file.read(buffer) != -1) out.write(buffer);
            return;
        }

should be:

        file.seek(0);
        if (neededAlignments.size() == 0) {
            // there is no needed alignment, stream it all!
            byte[] buffer = new byte[8192];
            int len;
            while (-1 != (len = file.read(buffer))){
                out.write(buffer, 0, len);
            }
            return;
        }

Original code would write extra data at the end in the most cases, except when file_size % 8192 == 0, which would cause apksigner to fail with Malformed APK: not a ZIP archive

Publish to maven

Publish this library to Maven. So that, it is easy to use it for everyone in their project without jar.

Ideas for the next 2.0.0

The current API of zipalign is very minimal and a bit too specific (the RandomAccessFile arg). Honestly the original API looks good as-is, and is perfect for its main goal but its a bit hard to maintain the long lines of code. I'm imagining a more object-oriented approach in the next 2.0.0 so it could be called a "real" library ๐Ÿ˜„

I'm thinking of an API that will look something like this:

ZipAnalyzer analyzer = new ZipAnalyzer("./file.zip");
// or 
ZipAnalyzer analyzer = new ZipAnalyzer(new FileInputStream(...));
// or maybe even
byte[] something = new byte[...];
ZipAnalyzer analyzer = new ZipAnalyzer(something);

analyzer.setAlignSoFiles(true, 4096);
analyzer.specialCase(new AlignCase() { ... }, 16); // perhaps the user wanted to align something specific? X)

Alignments alignments = analyzer.analyze();
alignments.align("./file.zip", "output.zip");
alignments.align(new FileInputStream(...), "output.zip");
alignments.alignInPlace("./file.zip");
byte[] result = alignments.alignToBytes("./file.zip");

What do you think?

Adding tests

Having tests would ensure zipalign-java works as expected. Lately many bugs have been occurring due to reckless merges by myself.

One question though: what kinds of tests should we do? zipaligning zips and shasum-ing them? if we were to, wouldn't introducing zips to the repo would make it heavier? i don't think people should get test zip files (that may be large) when they're cloning zipalign-java.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.