将波斯字符转换为UTF-8

时间:2018-02-17 09:56:19

标签: java string encoding character-encoding encode

我有一个波斯文件名,我想将其压缩(例如“تست.txt”是文件名)。在创建的zip文件中,波斯语字符变为“?”性格,我不知道为什么会这样。我已经尝试了很多方法将文件名转换为UTF-8,其中一种方法是使用URLEncoder将其作为utf-8。

    public void addFileToZip(InputStream file,ZipOutputStream zos,  String fileName )
            throws FileNotFoundException, IOException {
        System.setProperty("file.encoding", "UTF-8");
        fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");
        zos.putNextEntry(new ZipEntry(fileName));

        BufferedInputStream bis =
            new BufferedInputStream(file);

        long bytesRead = 0;
        byte[] bytesIn = new byte[BUFFER_SIZE];
         int read = 0;

        while ((read = bis.read(bytesIn)) != -1) {

            zos.write(bytesIn, 0, read);
            bytesRead += read;
        }
        zos.closeEntry();
        file.close();

    }

我称之为addFileTozip函数的主要方法是:

public static void main(String[] args) throws UnsupportedEncodingException {
String fileName = "D:\\testzip\\تست.txt";
String zipFileNameDir = "D:" + File.separator + "testzip" + File.separator + zipFileName;
 fileName = new String(zipFileNameDirr.getBytes("ISO-8859-1"), "UTF-8");
        try {
            InputStream fis = new FileInputStream(fileName);
            FileOutputStream fout = new FileOutputStream(zipFileNameDir);
            ZipOutputStream zos = new ZipOutputStream(fout);

            zipAllFiles.addFileToZip(fis , zos , fileInZipName);
            fis.close();
            zos.close();
            System.out.println("Exit method");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 

}

当我运行它时,programm抛出此异常:

java.io.FileNotFoundException: D:\testzip\???.txt (The filename, directory name, or volume label syntax is incorrect)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.io.FileInputStream.<init>(FileInputStream.java:101)
    at gampooya.tools.util.TestZipAllFiles.main(TestZipAllFiles.java:180)

这是一个Java程序而不是.net。

0 个答案:

没有答案