Hi!,
I've done a program that compress a tree directory into a zip file.
The problem is that when I decompressed that file, some filenames appear with a wrong filename. For example, files that contain ', ñ, (,)..., into his name.
I've forced the use of the UTF-8 charset but I also obtained the same result.
Debugging the program I can see that the zipOutputStream has the correct filename but when I open the file with winrar or winzip, the names appear wrong.
I will be eternally gratefully if someone can said me what is wrong into my code.
Thank you very much.
That's my code:
/*
* contiene el código que obtiene los parámetros y
* llama a FolderZiper
*/
public class zipUtil {
public static void main(String args[]) {
String[] aux = new String[2];
aux[0] = "C:\\MP3.zip";
aux[1] = "C:\\MP3O\\Queen\\03-Sheer heart attack(1974)";
args = aux;
String file = "";
if (args.length < 2 ){
System.out.println("sintaxis: " +
"zipUtil <nombre fichero ZIP> <nombre fichero 1>" +
"[<nombre fichero 2>...<nombre fichero n>]");
return;
}
file = args[0];
try {
FolderZiper.zipFolder(args[1], file);
}
catch(Exception e ) {
System.out.println("ERROR:" + e.getMessage());
}
}
}
import java.io.File;
import java.util.zip.ZipOutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import java.util.zip.ZipEntry;
/**
* FolderZiper provide a static method to zip a folder.
*
* @author pitchoun
*/
public class FolderZiper {
private static final String separador = File.separator;
private static final Charset UTF8 = Charset.forName("UTF-8");
/**
* Zip the srcFolder into the destFileZipFile. All the folder subtree of the src folder is added to the destZipFile
* archive.
*
* @param srcFolder String, the path of the srcFolder
* @param destZipFile String, the path of the destination zipFile. This file will be created or erased.
*/
static public void zipFolder(String srcFolder, String destZipFile) {
ZipOutputStream zip = null;
FileOutputStream fileWriter = null;
try {
fileWriter = new FileOutputStream(new String(destZipFile));
zip = new ZipOutputStream(fileWriter);
}
catch (Exception ex){
ex.printStackTrace();
System.exit(0);
}
addFolderToZip("", srcFolder, zip);
try {
zip.flush();
zip.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* Write the content of srcFile in a new ZipEntry, named path+srcFile, of the zip stream. The result
* is that the srcFile will be in the path folder in the generated archive.
*
* @param path String, the relatif path with the root archive.
* @param srcFile String, the absolute path of the file to add
* @param zip ZipOutputStram, the stream to use to write the given file.
*/
static private void addToZip(String path, String srcFile, ZipOutputStream zip) {
File folder = null;
folder = new File(new String(srcFile));
if (folder.isDirectory()) {
addFolderToZip(path, srcFile, zip);
}
else {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
String ruta = "";
try {
FileInputStream in = new FileInputStream(srcFile);
ruta = path + separador + new String(folder.getName());
zip.putNextEntry(new ZipEntry(ruta));
while ((len = in.read(buf)) > 0) {
zip.write(buf, 0, len);
}
}
catch (Exception ex){
ex.printStackTrace();
}
}
}
/**
* add the srcFolder to the zip stream.
*
* @param path String, the relatif path with the root archive.
* @param srcFile String, the absolute path of the file to add
* @param zip ZipOutputStram, the stream to use to write the given file.
*/
static private void addFolderToZip(String path, String srcFolder, ZipOutputStream zip) {
File folder = null;
String ruta = "";
String ruta2 = "";
folder = new File(new String(srcFolder));
String fileListe[] = folder.list();
try {
int i = 0;
while (true) {
ruta = path + separador + new String(folder.getName());
ruta2 = srcFolder + separador + new String(fileListe);
addToZip(ruta, ruta2, zip);
i++;
}
}
catch (Exception ex) {
}
}
}