How to use LZMA SDK to compress/decompress in Java

后端 未结 6 1076
灰色年华
灰色年华 2020-12-04 17:03

http://www.7-zip.org/sdk.html This site provide a LZMA SDK for compress/decompress files, I would like to give it a shot but I am lost.

Anyone got experience on this

6条回答
  •  再見小時候
    2020-12-04 17:41

    https://mvnrepository.com/artifact/org.tukaani/xz/1.8

    Kotlin code for Android:

     fun initDatabase() {
        var gisFile = this.getDatabasePath("china_gis.db");
        if (!gisFile.exists()) {
            if(!gisFile.parentFile.exists()) gisFile.parentFile.mkdirs();
            var inStream = assets.open("china_gis_no_poly.db.xz")
            inStream.use { input ->
                val buf = ByteArray(1024)
                XZInputStream(input).use { input ->
                    FileOutputStream(gisFile,true).use { output ->
                        var size: Int
                        while (true) {
                            size = input.read(buf);
                            if (size != -1) {
                                output.write(buf, 0, size)
                            } else {
                                break;
                            }
                        }
                        output.flush()
                    }
    
                }
            }
        }
    }
    

    Java code:

     byte[] buf = new byte[8192];
        String name =  "C:\\Users\\temp22\\Downloads\\2017-007-13\\china_gis_no_poly.db.xz";
        try {
    
          InputStream input = new FileInputStream(name);
                    FileOutputStream output=  new FileOutputStream(name+".db");
                    try {
                        // Since XZInputStream does some buffering internally
                        // anyway, BufferedInputStream doesn't seem to be
                        // needed here to improve performance.
                        // in = new BufferedInputStream(in);
                        input = new XZInputStream(input);
    
                        int size;
                        while ((size = input.read(buf)) != -1)
                            output.write(buf, 0, size);
                        output.flush();
    
                    } finally {
                        // Close FileInputStream (directly or indirectly
                        // via XZInputStream, it doesn't matter).
                        input.close();
                        output.close();
                    }
    
        } catch (FileNotFoundException e) {
            System.err.println("XZDecDemo: Cannot open " + name + ": "
                               + e.getMessage());
            System.exit(1);
    
        } catch (EOFException e) {
            System.err.println("XZDecDemo: Unexpected end of input on "
                               + name);
            System.exit(1);
    
        } catch (IOException e) {
            System.err.println("XZDecDemo: Error decompressing from "
                               + name + ": " + e.getMessage());
            System.exit(1);
        }
    

提交回复
热议问题