Bonjour,
J'utilise le programme ci_dessous pour dé-zipper un fichier (cela marche très bien avec des logiciels tels que WinRar ou Unzip).
En fait il s'agit d'un code que j'ai trouvé sur le Net. J'obtiens le message "MAL FORMED".
J'ai essayé avec d'autres codes que j'ai également trouvés et j'obtiens des erreurs différentes "File too large" par exemple.
En fait il s'agit d'un fichier zippé (Bataille.zip, que je joins également) relativement petit : 76K, incluant 16 fichiers, chacun d'eux ne dépassant pas 37K.
Dans mon code vous trouverez des traces (messages à la console) pour savoir sur quelle instruction il se plantait, il s'agit de entry = zipIn.getNextEntry(); .
Avez-vous des suggestions à me faire, ou un exemple qui pourrait fonctionner correctement.
Pour Info: je développe sous Windows 8.1 avec Eclipse en mode 64 bits.
Merci d'avance.
Gégé
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class UnzipFile { List<String> fileList; static final int BUFFER = 512; static final int TOOBIG = 0x6400000; // Max size of unzipped data, 100MB static final int TOOMANY = 1024; // Max number of files // ... private static final String outputFolder = System.getProperties().getProperty("user.dir") ; public static String zipFile = ""; private static final int BUFFER_SIZE = 64096; // j'ai essayé avec 32K et moins. public UnzipFile(String fichier) throws Exception { super(); zipFile = fichier; unzip(zipFile, outputFolder); } public void unzip (String zipFilePath, String destDirectory) { try { File destDir = new File(destDirectory); if (!destDir.exists()) { destDir.mkdir(); } System.out.println("1" ); ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); System.out.println("2" ); ZipEntry entry = zipIn.getNextEntry(); System.out.println("3" ); // iterates over entries in the zip file while (entry != null) { System.out.println(entry.getName() ); String filePath = destDirectory + File.separator + entry.getName(); if (!entry.isDirectory()) { // if the entry is a file, extracts it System.out.println("4" ); extractFile(zipIn, filePath); System.out.println("5" ); } else { // if the entry is a directory, make the directory File dir = new File(filePath); dir.mkdir(); } System.out.println("6" ); zipIn.closeEntry(); System.out.println("7" ); entry = zipIn.getNextEntry(); System.out.println("8" ); } zipIn.close(); } catch (Exception ex){System.out.println(ex.toString());} } private void extractFile(ZipInputStream zipIn, String filePath) throws IOException { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); byte[] bytesIn = new byte[BUFFER_SIZE]; int read = 0; while ((read = zipIn.read(bytesIn)) != -1) { bos.write(bytesIn, 0, read); } bos.close(); } public static void main( String[] args ) throws Exception { new UnzipFile("F:\\Test\\Bataille.zip"); } }
Partager