2011-10-05 60 views
0

我需要讀取一個txt文件,將其從UTF8轉換爲ISO8859-1並將文本文件存儲到一個zip文件中。從txt文件中壓縮一個字節[]

這是我走到這一步:

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文件,看了一遍和zip它與ZipOutputStream書房? 或者有沒有辦法使用txtfile的byte []來創建zip?

+0

ZipOutputStream.write需要一個字節數組。 –

回答

2

簡短的回答是肯定的,你可以直接從字節數組中寫入zip。

看一看:java.util.zip.ZipOutputStream.write()的文檔。它需要一個字節數組,起始偏移量和長度,因爲它是參數。

應該從這裏自我解釋。

只是爲了提醒你,並不是所有的UTF-8都可以編碼爲ISO8859-1。

相關問題