Java 从txt文件压缩字节[]
我需要读取一个txt文件,将其从UTF8转换为ISO8859-1,并将该文本文件存储到zip中 到目前为止,我得到的是: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
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接受字节数组。