Bonjour à tous,
Voici mon problème.
J'ai un fichier zip tot.zip dans le quel se trouve 2000 fichiers. Il y a un fichier en particulier tata.txt que j'aimerai dézipper, modifier et remettre dans mon zip avec les modifications apportées.
Dézipper tata.txt => OK
Modifier tata.txt => OK
Remettre tata.txt dans toto.zip en remplaçant l'ancien tata.txt => NOT OK.
Est ce que quelqu'un à déjà eu et résolu ce pb ?
Bien sur j'aimerais faire cela sans avoir à dézipper tout mon fichier toto.zip, mais est ce possible ?
Pour l'instant j'utilise le code suivant mais celui ci me renvoie une erreur lorsqu'il veut ajouter un fichier qui existe déjà dans le zip. Peut être que ce que je veux faire n'est pas possible...
Merci de votre aide.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public void addFilesToExistingZip(File zipFile, String file, boolean fullPath) throws IOException { // get a temp file File tempFile = File.createTempFile(zipFile.getName(), null); // delete it, otherwise you cannot rename your existing zip to it. tempFile.delete(); boolean renameOk=zipFile.renameTo(tempFile); if (!renameOk) { throw new RuntimeException("could not rename the file "+zipFile.getAbsolutePath()+" to "+tempFile.getAbsolutePath()); } byte[] buf = new byte[1024]; ZipInputStream zin = new ZipInputStream(new FileInputStream(tempFile)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile)); ZipEntry entry = zin.getNextEntry(); while (entry != null) { String name = entry.getName(); boolean notInFiles = true; if (file.equals(name)) { notInFiles = false; break; } if (notInFiles) { // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(name)); // Transfer bytes from the ZIP file to the output file int len; while ((len = zin.read(buf)) > 0) { out.write(buf, 0, len); } } entry = zin.getNextEntry(); } // Close the streams zin.close(); // Compress the file InputStream in = new FileInputStream(file); // Add ZIP entry to output stream. if (fullPath) { out.putNextEntry(new ZipEntry(file)); } else { out.putNextEntry(new ZipEntry(new File(file).getName())); } // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); // Complete the ZIP file out.close(); tempFile.delete(); }
N'hésitez pas à me questionner si cela n'est pas clair.
Partager