簡體   English   中英

壓縮到文件的JAVA char []

[英]JAVA char[] to Zip to File

我有一個chararray,它在每個索引中保存坐標。 我想用zip算法壓縮數組,並將數組的壓縮版本寫入文件。 我的想法是,我通過zip流運行chararray,然后將其直接寫入test.txt等文件。 問題是,在執行代碼后沒有任何內容寫入文件。 有人可以幫我解決這個問題嗎? 親切的問候洛倫佐

這是我目前的代碼:

    byte[] bytes = Charset.forName("UTF-8").encode(CharBuffer.wrap(sample)).array();
    FileOutputStream out = new FileOutputStream(cscFile, true);
    byte[] compressed = new byte[bytes.length];
    ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
    ZipInputStream zi = new ZipInputStream(bi);
    ZipEntry entry = null;
    while ((entry = zi.getNextEntry()) != null) {
    zi.read(compressed);
    for(int i = 0; i<bytes.length;i++){
    out.write(compressed[i]);
    }

    out.flush();
    out.close();
    zi.closeEntry();
}
zi.close(); 

您可能需要稍微修改一下這樣的代碼:

    ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File("your zip file name")));
    ZipEntry entry = new ZipEntry("zipped file name");
    entry.setSize(bytes.length);
    zos.putNextEntry(entry);
    zos.write(bytes);
    zos.closeEntry();
    zos.close();

說明:要壓縮寫入數據,請使用ZipOutputStream並將創建的FileOutputStream作為構造函數參數。 之后,創建ZipEntry,它表示zip文件中的文件並將字節數組的內容寫入其中。

希望能幫助到你。

在這里查看類似的問題: https//stackoverflow.com/a/357892/3115098

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM