problème path dans fichier zip
Désolé, même message que précédent :oops:
Bonjour,
J'ai un petit problème de création d'un fichier zip avec java.
Tout se passe correctement, et le fichier zip est bien créé, mais en le décompressant on voit toute toute l'arborescence à partir de la racine :
/Users/toto/Desktop/mondossier/fichiers
Il y a t-il un moyen de ziper un dossier à partir d'un dossier parent, dans mon exemple à partir de "Desktop" ?
voici mon main :
Code:
1 2 3 4 5 6 7
|
public static void main(String[] args) throws IOException {
Compress compress = new Compress();
compress.zipDirectory("/Users/toto/Desktop/mondossier","mondossier");
} |
et ma classe de compression
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
public static void zipDirectory(String dir, String zipfile)
throws IOException, IllegalArgumentException {
// Check that the directory is a directory, and get its contents
File d = new File(dir);
if (!d.isDirectory())
throw new IllegalArgumentException("Not a directory: "
+ dir);
String[] entries = d.list();
byte[] buffer = new byte[4096]; // Create a buffer for copying
int bytesRead;
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
for (int i = 0; i < entries.length; i++) {
File f = new File(d, entries[i]);
if (f.isDirectory())
continue;//Ignore directory
FileInputStream in = new FileInputStream(f); // Stream to read file
ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry
out.putNextEntry(entry); // Store entry
while ((bytesRead = in.read(buffer)) != -1)
out.write(buffer, 0, bytesRead);
in.close();
}
out.close();
} |
Merci
Jean-Pierre Astier