tukaani-project / xz-java

XZ for Java
https://tukaani.org/xz/java.html
BSD Zero Clause License
23 stars 14 forks source link

[Bug]: A strange format error in the generated xz file due to spaces #6

Closed RaymondLam1 closed 2 months ago

RaymondLam1 commented 11 months ago

Describe the bug

I wrote a very simple program that uses json strings to generate an xz compressed file, but I found a puzzling bug.

    @Test
    public void testXzCompress() throws IOException {
        String json = new String(Files.readAllBytes(
                Paths.get("/Users/raymond/jdks.json")));
        compress(json);
    }

    public void compress(String input) {
        try (FileOutputStream fos = new FileOutputStream("/Users/raymond/ztest/tmp3/jdks.json.xz");
                XZOutputStream xzOut = new XZOutputStream(fos, new LZMA2Options())) {
            byte[] jsonData = input.getBytes();
            xzOut.write(jsonData);
        } catch (Throwable throwable) {
            throw new RuntimeException(throwable);
        }
    }

When jdks.json is below, It will cause Error 79 - Inappropriate file type or format. when trying to open the generated jdks.json.xz in my mac.

{
  "jdks":[
    {
      "vendor":"Oracle"
    }
  ]
}

However, when jdk.json is below, The only difference is that a space is added after "jdks", It works well.

{
  "jdks": [
    {
      "vendor":"Oracle"
    }
  ]
}

I'm not sure if this is a bug or a usage error on my part.

Version

1.9

Operating System

macOS Monterey

Relevant log output

No response

Larhzu commented 6 months ago

Sorry for the delayed reply. Turns out I didn't have email notifications enabled for the xz-java project.

The problem is that your code lacks xzOut.close().

If you didn't want to close fos then xzOut.finish() would be the right call. But in this example you want to close fos too and xzOut.close() does that.

RaymondLam1 commented 6 months ago

Sorry for the delayed reply. Turns out I didn't have email notifications enabled for the xz-java project.

The problem is that your code lacks xzOut.close().

If you didn't want to close fos then xzOut.finish() would be the right call. But in this example you want to close fos too and xzOut.close() does that.

Thanks for your reply, I will try it.

RaymondLam1 commented 6 months ago

Unfortunately, xzOut.close() does not work. Actually, I use try with catch, so xzOut.close() is unnecessary. Maybe it is a bug ?

Larhzu commented 6 months ago

Actually, I use try with catch, so xzOut.close() is unnecessary.

True, I didn't notice it, sorry. I haven't regularly used much beyond Java 5 features yet and I had a break from Java for three years. :-/

If I compress and decompress your two example JSON files, everything works correctly. Based on your code, I created a complete program that compresses to jdks.json.xz and the output is fine with both of your sample inputs. Thus, I cannot reproduce the problem with the information I currently have.

Note that the error message

Error 79 - Inappropriate file type or format.

is not from XZ for Java.

import java.io.; import java.nio.file.; import org.tukaani.xz.*;

class XZEncDemo { public void testXzCompress() throws IOException { String json = new String(Files.readAllBytes( Paths.get("jdks.json"))); compress(json); }

public void compress(String input) {
    try (FileOutputStream fos = new FileOutputStream("jdks.json.xz");
            XZOutputStream xzOut = new XZOutputStream(fos, new LZMA2Options())) {
        byte[] jsonData = input.getBytes();
        xzOut.write(jsonData);
    } catch (Throwable throwable) {
        throw new RuntimeException(throwable);
    }
}

public static void main(String[] args) throws Exception {
    new XZEncDemo().testXzCompress();
}

}

Larhzu commented 2 months ago

I'm closing this because I cannot reproduce it and I don't have a reason to suspect a bug at XZ for Java. Please reopen if you have more information, especially if it is possible to have a small program to reproduce the issue. Thanks.