0

I have a Persian file name which I want to zip it(for example "تست.txt" is the file name). In created zip file persian characters turns to "?" character and I don't know why is this happening. I've tried many ways to convert the file name to UTF-8 and one of those ways was using URLEncoder to make it as 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();

    }

main method which I call addFileTozip function is:

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();
        } 

}

When I run it programm throws this exception:

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)

This is a Java program not .net.

Amir Amiri
  • 441
  • 1
  • 8
  • 14
  • Have a look on this answer https://stackoverflow.com/q/27500358/2343086, you might need code page 1097 – abdul qayyum Feb 17 '18 at 10:19
  • Possible duplicate of [DotNetZip zipping files with Arabic names](https://stackoverflow.com/questions/27500358/dotnetzip-zipping-files-with-arabic-names) – abdul qayyum Feb 17 '18 at 10:21
  • @abdulqayyum https://stackoverflow.com/questions/27500358/dotnetzip-zipping-files-with-arabic-names This is in .Net not java so it doesn't work in java. – Amir Amiri Feb 17 '18 at 10:34
  • You might want to change your question and describe the real problem. I don't understand why you are URL encoding. – Mark Rotteveel Feb 17 '18 at 10:43
  • @MarkRotteveel I edited question. I searched for solutions of converting file name to UTF-8 and one of those solutions was using URLEncoding. – Amir Amiri Feb 17 '18 at 10:50
  • That makes no sense at all, you're trying to access a file on your local system (I assume based on the file path), then URL encoding it doesn't work. It seems that in your attempt to solve your problem, you have taken a wrong turn and now your focused too much on that, and not on the real problem you're trying to solve. Please edit your question to describe your real problem (the thing you're trying to do), the code you tried and the full exception stacktrace. – Mark Rotteveel Feb 17 '18 at 10:53
  • @MarkRotteveel I edited question again. thanks for your help. – Amir Amiri Feb 17 '18 at 11:02
  • 1
    Why are you doing `new String(fileName.getBytes("UTF-8"), "UTF-8")` and `new String(zipFileNameDirr.getBytes("UTF-8"), "ISO-8859-1")`? That makes no sense, the first will lead to the same string, and the second will just lead to garbage for non-ASCII characters, as ISO-8859-1 doesn't contain Persian characters. – Mark Rotteveel Feb 17 '18 at 11:08
  • Repeating for emphasis. **new String(fileName.getBytes("ISO-8859-1"), "UTF-8")` is simply, always wrong!** – Tom Blodget Feb 18 '18 at 22:01
  • `zos.putNextEntry(new ZipEntry("تست.txt"))` works just fine. It takes a String (using the UTF-16 encoding of the Unicode character set per the Java spec) and converts it to a file entry (using the UTF-8 encoding of the Unicode character set per the PKZip spec). Perhaps your test method is invalid. How did you initially perceive a problem (and with what code)? – Tom Blodget Feb 18 '18 at 22:07

0 Answers0