Bonjour,
Je voudrais pouvoir mettre des dossiers et fichiers dans un fichier.tar qui n'est pas compresser, seulement le .tar.
Je ne trouve pas un moyen simple à comprendre sur le web, quelqu'un aurait il des suggestion svp ?
Merci d'avance
Version imprimable
Bonjour,
Je voudrais pouvoir mettre des dossiers et fichiers dans un fichier.tar qui n'est pas compresser, seulement le .tar.
Je ne trouve pas un moyen simple à comprendre sur le web, quelqu'un aurait il des suggestion svp ?
Merci d'avance
Le plus simple est un appel système à la commande "tar" si elle est disponible sur ton système.
Sinon il y a une foultitude d'API pour générer le format "tar" en Java.
Enfin, si tu as du temps et de la curiosité, tu peux implémenter toi-même le code qui génère le format "tar" depuis plusieurs fichiers. Ce n'est pas compliqué et c'est amusant (pour peu qu'on ai le même humour) ;)
Tu aurait des exemples stp ?Citation:
Sinon il y a une foultitude d'API pour générer le format "tar" en Java.
:google2:
Avec google, comme dit par Dinobogan, en 1 click tu trouves une lib toute faite, mais c'est a mon avis faut d'avoir voulu chercher ... https://code.google.com/p/jtar/
Et sinon en utilisant directement la commande tar, un appel a 'tar --help | head' donne une exemple.
Merci mais j'ai déjà trouvé ce code, il ne marche pas.
Le problème est cette ligne
Malgré le fait d'avoir importé java.util.zip.*Code:
1
2 TarOutputStream out = new TarOutputStream( new BufferedOutputStream( dest ) );
Je vais donc faire des recherches sur la commend alors.
En attendant si vous avez la solution du pourquoi eclipse ne reconnait pas TarOutPutStream
Merci
J'ai adapté un nouveau code pour créer un tar :
Voici ce qui doit être dans le tarCode:
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 //problème : http://www.developpez.net/forums/d1529048/java/general-java/apis/documents/creer-fichier-tar/#post8299726 //commande tar : http://www-igm.univ-mlv.fr/~duris/ENPC/archive.html package pSignature.pInclude; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.nio.file.Path; public class FileTar { /** * tar the agents * @param fichierCopier agent files * @param outputFile agent tar * @param baseDir the directory path to run "tar" command */ public static void tarFile(String fichierCopier,Path outputFile,String baseDir){ String cmd="tar -zcf "+outputFile+" "; //for (int i = 0; i < inputFiles.length; i++) { cmd+=fichierCopier+" "; //} System.out.println(cmd); try { Process process=Runtime.getRuntime().exec(cmd, null, new File(baseDir)); BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(process.getInputStream())); String s; while ((s=bufferedReader.readLine())!=null) { System.out.println(s); } process.waitFor(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
et pour finir le message d'erreurCode:
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 package pSignature.pInclude; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import pSignature.fSignature; public class Repertoire { private static String reference_indice; private static String reference_409_indice_gz; private static String folderName; private static String filePath; private static String chemin; public static void concepteurData(Object reference, Object indice, String fichierCopier) throws IOException { //creer le dossier parent reference_indice = reference + "_" + indice; //creer le sous dossier reference_409_indice_gz = reference + "_409_" + indice + "_gz"; //chemin du sous dossier chemin = reference_indice + "/" + reference_409_indice_gz; //vérification avant la création if(!new File(reference_indice).exists()) { Path monRepertoire = Paths.get(reference_indice.toUpperCase()); Files.createDirectory(monRepertoire); //répertoire temporaire Path repertoireTemp = Files.createTempDirectory(monRepertoire + "_"); System.out.println("Le répertoire temporaire " +repertoireTemp + " Ã* bien été crée"); System.out.println("Le répertoire " + reference_indice.toUpperCase() + " Ã* bien été créé"); } else System.out.println("Le répertoire " + reference_indice.toUpperCase() + " existe déjÃ*"); if (!new File(chemin).exists()) { //new File(chemin.toUpperCase()).mkdirs(); Path monSousRepertoire = Paths.get(chemin.toUpperCase()); Files.createDirectories(monSousRepertoire); //créer le fichier corespondant au dossier folderName = reference_409_indice_gz.toUpperCase(); filePath = reference_indice + "/" + folderName.toUpperCase() + ".txt"; fSignature.creerFichierText409(folderName, filePath); } else { System.out.println("Le fichier " + folderName.toUpperCase() + " existe déjÃ* dans " + filePath.toUpperCase()); } //emplacement du du fichier Ã* coller Path monFichierCopier = Paths.get(fichierCopier); Path fichierColler = Paths.get(chemin).resolve(monFichierCopier.getFileName()); // attention chemin est relatif : il sera donc concaténé au dossier courant d'exécution... Files.copy(monFichierCopier, fichierColler); FileTar.tarFile(fichierCopier, fichierColler, folderName); } }
Merci encore pour votre aide...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
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 Le répertoire temporaire D:\Users\T0163126\AppData\Local\Temp\123_-A_6166820720051310569 Ã* bien été crée Le répertoire 123_-A Ã* bien été créé Le fichier 123_409_-A_GZ a été créé ! tar -zcf 123_-a\123_409_-a_gz\mise_en_approbation.tcl D:\Users\T0163126\Documents\mise_en_approbation.tcl java.io.IOException: Cannot run program "tar" (in directory "123_409_-A_GZ"): CreateProcess error=267, Nom de répertoire non valide at java.lang.ProcessBuilder.start(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at java.lang.Runtime.exec(Unknown Source) at pSignature.pInclude.FileTar.tarFile(FileTar.java:28) at pSignature.pInclude.Repertoire.concepteurData(Repertoire.java:57) at pSignature.fSignature.validation(fSignature.java:425) at pSignature.fSignature$2.actionPerformed(fSignature.java:124) at javax.swing.AbstractButton.fireActionPerformed(Unknown Source) at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source) at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Caused by: java.io.IOException: CreateProcess error=267, Nom de répertoire non valide at java.lang.ProcessImpl.create(Native Method) at java.lang.ProcessImpl.<init>(Unknown Source) at java.lang.ProcessImpl.start(Unknown Source) ... 44 more Le fichier 123_-A.txt a été créé ! Le fichier support.txt a été créé !
Bonjour,
Voici la class Tar
il me renvoi ceci :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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FileTar { /** * tar the agents * @param monRepertoire agent files * @param chemin agent tar * @param baseDir the directory path to run "tar" command */ public static void tarFile(Path monRepertoire,Path chemin,String baseDir){ String cmd= "cmd /C " + baseDir + "\\tar.exe -cf "+chemin+" "; //for (int i = 0; i < inputFiles.length; i++) { cmd+=monRepertoire+" "; //} System.out.println(cmd); try { Process process=Runtime.getRuntime().exec(cmd, null, new File(baseDir)); BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(process.getInputStream())); String s; while ((s=bufferedReader.readLine())!=null) { System.out.println(s); } process.waitFor(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { Path monRepertoire = Paths.get("approbateur.txt"); Path tarPath = Paths.get("monTar.tar"); String tarExecPath = "D:\\Users\\T0163126\\Documents\\Mes Outils Personnels\\WBin\\"; try { FileTar.tarFile(monRepertoire, tarPath, tarExecPath); } catch (Exception e) { e.toString(); } } }
mais rien ne se passe, quelqu'un serait il pourquoi ?Code:
1
2 cmd /C D:\Users\T0163126\Documents\Mes Outils Personnels\WBin\\tar.exe -cf monTar.tar approbateur.txt
Executer a la main, dans une console cette commande raconte quoi ?