簡體   English   中英

如何在java中解壓縮不是UTF8格式的文件

[英]How to unzip file that that is not in UTF8 format in java

我有一個文件,例如test.zip。 如果我使用像winrar這樣的ZIP工具,則很容易提取(將test.zip解壓縮到test.csv)。 但test.csv不是UTF8格式。 我的問題是,當我使用java解壓縮它時,它無法讀取此文件。

ZipFile zf = new ZipFile("C:/test.zip");

拋出的異常表示通過打開該文件會發生錯誤。

在java http://java.sun.com/developer/technicalArticles/Programming/compression/上沒有關於數據格式化的文章。 也許整個API僅針對UTF8格式數據而設計。 那么,如果我必須解壓縮除UTF8格式之外的數據,如何解壓縮呢? 特別是擁有更多空間大小的日文和中文字符(UTF8除外)。 我還在http://truezip.java.net/6/tutorial.html找到了一個API,其中提到了這個問題。 但是,我沒有找到解決方法。 有沒有簡單的方法來解決這個問題? 特別是從JAVA規范請求傳遞的API。

JDK6在java.util.zip實現中有一個錯誤,它無法處理非USASCII字符。 我使用Apache Commons commons-compress-1.0.jar庫來修復它。 JDK7修復了java.util.zip的實現。 http://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipInputStream.html

import java.io.*;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.*;

public static int unzip(File inputZip, File outputFolder) throws IOException {
    int count=0;
    FileInputStream fis = null;
    ZipArchiveInputStream zis = null;
    FileOutputStream fos = null;
    try {
        byte[] buffer = new byte[8192];
        fis = new FileInputStream(inputZip);
        zis = new ZipArchiveInputStream(fis, "Cp1252", true); // this supports non-USACII names
        ArchiveEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            File file = new File(outputFolder, entry.getName());
            if (entry.isDirectory()) {
                file.mkdirs();
            } else {
                count++;
                file.getParentFile().mkdirs();
                fos = new FileOutputStream(file);
                int read;
                while ((read = zis.read(buffer,0,buffer.length)) != -1)
                    fos.write(buffer,0,read);
                fos.close();
                fos=null;
            }
        }
    } finally {
        try { zis.close(); } catch (Exception e) { }
        try { fis.close(); } catch (Exception e) { }
        try { if (fos!=null) fos.close(); } catch (Exception e) { }
    }
    return count;
}

不,zip文件不僅僅用於UTF-8的數據。 Zip文件根本不會嘗試解釋文件中的數據,Java API也不會。

圍繞文件的非ASCII 名稱可能存在問題,但文件內容本身應該不是問題。 在您的情況下,看起來文件的名稱只是test.zip ,因此您不應該遇到任何名稱編碼問題。

如果文件無法打開 ,那么聽起來你有一個不同的問題。 你確定文件存在於你期望的位置嗎?

你能嘗試下面的代碼嗎? 有關更多示例,請訪問http://java2novice.com/java-collections-and-util/zip/unzip/

FileInputStream fis = null;
    ZipInputStream zipIs = null;
    ZipEntry zEntry = null;
    try {
        fis = new FileInputStream(filePath);
        zipIs = new ZipInputStream(new BufferedInputStream(fis));
        while((zEntry = zipIs.getNextEntry()) != null){
            try{
                byte[] tmp = new byte[4*1024];
                FileOutputStream fos = null;
                String opFilePath = "C:/"+zEntry.getName();
                System.out.println("Extracting file to "+opFilePath);
                fos = new FileOutputStream(opFilePath);
                int size = 0;
                while((size = zipIs.read(tmp)) != -1){
                    fos.write(tmp, 0 , size);
                }
                fos.flush();
                fos.close();
            } catch(Exception ex){

            }
        }
        zipIs.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

我記得這只發生在文件名未以UTF8編碼時。

如果未禁止第3個組件,請嘗試使用Apache Zip API。

import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile;

嘗試使用此代碼,我用它來提取所有的zip文件

try
    {

        final ZipFile zf = new ZipFile("C:/Documents and Settings/satheesh/Desktop/POTL.Zip");

        final Enumeration<? extends ZipEntry> entries = zf.entries();
        ZipInputStream zipInput = null;

        while (entries.hasMoreElements())
        {
            final ZipEntry zipEntry=entries.nextElement();
            final String fileName = zipEntry.getName();
        // zipInput = new ZipInputStream(new FileInputStream(fileName));
            InputStream inputs=zf.getInputStream(zipEntry);
            //  final RandomAccessFile br = new RandomAccessFile(fileName, "r");
                BufferedReader br = new BufferedReader(new InputStreamReader(inputs, "UTF-8"));
                FileWriter fr=new FileWriter(f2);
            BufferedWriter wr=new BufferedWriter(new FileWriter(f2) );

            while((line = br.readLine()) != null)
            {
                wr.write(line);
                System.out.println(line);
                wr.newLine();
                wr.flush();
            }
            br.close();
            zipInput.closeEntry();
        }


    }
    catch(Exception e)
    {
        System.out.print(e);
    }
    finally
    {
        System.out.println("\n\n\nThe had been extracted successfully");

    }

這段代碼真的很有效。

暫無
暫無

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

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