iyxan23 / zipalign-java

zipalign implemented as a java library with 0 deps
MIT License
54 stars 13 forks source link

Wrong file is produced when align is not needed #11

Closed cr4zyserb closed 6 months ago

cr4zyserb commented 6 months ago

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

iyxan23 commented 6 months ago

Thanks for the catch! I wasn't very familiar with Java streams at the time so that was a mistake on my part 😅

Would you be kind enough to make a PR instead? So that'd count as a contribution. Thanks!

cr4zyserb commented 6 months ago

You're welcome. I think it is easier if you fix it directly in the code. If you prefer that I do PR, I can do it, but it is such a minor fix that it is not even worth considering it as a contribution :smiley:

iyxan23 commented 6 months ago

Sure, if you said so! Thanks for the fix