Bonjour ou plutôt bonsoir
En fin de post, un petit programme de test permettant de créer une archive ZIP. Je suis ouvert aux remarques / suggestions / améliorations quant au code, mais j'ai surtout une question s'agissant du second paramètre à fournir à la fonction FileSystems.newFileSystem(). Outre la propriété "create" à "true", y-a-t-il d'autres propriétés qu'il conviendrait d'ajouter ? Dans quelques bouts de code trouvés sur le net, j'ai vu par exemple "encoding" à la valeur "UTF-8", mais ça semble marcher sans ? Quelles propriétés utilisez-vous, dans quelles situations ? Existe-t-il une page listant les propriétés utiles pour travailler avec des fichiers ZIP ?
D'avance merci !
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 package testjava2; import java.io.IOException; import java.net.URI; import java.nio.file.DirectoryStream; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Map; public class TestJava2 { private static final Path PATH_FICHIER_ZIP = Paths.get( "D:\\test_creation.zip" ); private static final Path PATH_DOSSIER_SRC = Paths.get( "D:\\projet\\www\\monsite" ); private static final int PROFONDEUR_DOSSIER_SOURCE = PATH_DOSSIER_SRC.getNameCount(); private static ArrayList<Path> scanDossier(Path path) throws IOException { ArrayList<Path> liste = new ArrayList<>(); try( DirectoryStream<Path> ds = Files.newDirectoryStream( path ) ) { for( Path pathEnCours : ds ) { liste.add( pathEnCours ); if( Files.isDirectory(pathEnCours)) liste.addAll( scanDossier(pathEnCours) ); } } return liste; } public static void main(String[] args) { System.out.println("Création de l'archive : " + PATH_FICHIER_ZIP.toString() ); System.out.println("Dossier source : " + PATH_DOSSIER_SRC.toString() ); // ZIP : URI du fichier à créer // jar:file:///D:/test_creation.zip URI zipURI = URI.create("jar:" + PATH_FICHIER_ZIP.toUri().toString()); // ZIP : Map of provider specific properties to configure the file system Map<String, String> zipProp = new HashMap<>(); zipProp.put( "create", "true"); try( FileSystem zipFS = FileSystems.newFileSystem(zipURI, zipProp) ) { // Scan du dossier source et tri des items (fichiers et dossiers) ArrayList<Path> arrPath = scanDossier(PATH_DOSSIER_SRC); Collections.sort( arrPath ); // Ajout au fichier ZIP for(Path pathSource : arrPath ) { Path pathInterneZIP = zipFS.getPath( pathSource.subpath( PROFONDEUR_DOSSIER_SOURCE, pathSource.getNameCount() ).toString() ); System.out.println( pathSource + " >>> " + pathInterneZIP); Files.copy(pathSource, pathInterneZIP); } System.out.println("Terminé"); } catch( IOException e ) { System.out.println("Une erreur est survenue : " +e.toString()); } } }
Partager