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.