Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从txt文件压缩字节[]_Java - Fatal编程技术网

Java 从txt文件压缩字节[]

Java 从txt文件压缩字节[],java,Java,我需要读取一个txt文件,将其从UTF8转换为ISO8859-1,并将该文本文件存储到zip中 到目前为止,我得到的是: Charset utf8charset = Charset.forName("UTF-8"); Charset iso88591charset = Charset.forName("ISO-8859-1"); File file_in = new File("Input/file1.txt"); File file_out = new File("Output/file_o

我需要读取一个txt文件,将其从UTF8转换为ISO8859-1,并将该文本文件存储到zip中

到目前为止,我得到的是:

Charset utf8charset = Charset.forName("UTF-8");
Charset iso88591charset = Charset.forName("ISO-8859-1");

File file_in = new File("Input/file1.txt");
File file_out = new File("Output/file_out.txt");

try {
    FileInputStream fis = new FileInputStream(file_in);
    BufferedInputStream bis = new BufferedInputStream(fis);
    byte fileContent[] = new byte[(int)file_in.length()];

    bis.read(fileContent);

    ByteBuffer bb = ByteBuffer.wrap(fileContent);
    CharBuffer data = utf8charset.decode(bb);
    ByteBuffer outputBuffer = iso88591charset.encode(data);
    byte outputData[] = outputBuffer.array();

    FileOutputStream fos = new FileOutputStream(file_out);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bos.write(outputData);
    bos.close();

} catch ...
我必须创建txt文件,再次读取并使用ZipoutStream压缩它吗?
或者有没有一种方法可以使用txtfile的byte[]来创建zip???

简短回答是的,您可以直接从字节数组写入zip

查看以下文档:
java.util.zip.ZipOutputStream.write()
。它采用字节数组、起始偏移量和长度作为参数

从那以后应该是不言自明的


只是提醒一下,并非所有UTF-8都可以编码为ISO8859-1

ZipOutputStream.write接受字节数组。