IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Documents Java Discussion :

Comment créer un fichier.tar


Sujet :

Documents Java

  1. #1
    Nouveau membre du Club
    Homme Profil pro
    Etudiant en informatique
    Inscrit en
    Juillet 2013
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Etudiant en informatique

    Informations forums :
    Inscription : Juillet 2013
    Messages : 102
    Points : 34
    Points
    34
    Par défaut Comment créer un fichier.tar
    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

  2. #2
    Modérateur
    Avatar de dinobogan
    Homme Profil pro
    ingénieur
    Inscrit en
    Juin 2007
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 4 073
    Points : 7 163
    Points
    7 163
    Par défaut
    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)
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java
    Que la force de la puissance soit avec le courage de ta sagesse.

  3. #3
    Nouveau membre du Club
    Homme Profil pro
    Etudiant en informatique
    Inscrit en
    Juillet 2013
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Etudiant en informatique

    Informations forums :
    Inscription : Juillet 2013
    Messages : 102
    Points : 34
    Points
    34
    Par défaut
    Sinon il y a une foultitude d'API pour générer le format "tar" en Java.
    Tu aurait des exemples stp ?

  4. #4
    Modérateur
    Avatar de dinobogan
    Homme Profil pro
    ingénieur
    Inscrit en
    Juin 2007
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France

    Informations professionnelles :
    Activité : ingénieur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2007
    Messages : 4 073
    Points : 7 163
    Points
    7 163
    Par défaut
    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java
    Que la force de la puissance soit avec le courage de ta sagesse.

  5. #5
    Modérateur
    Avatar de wax78
    Homme Profil pro
    Chef programmeur
    Inscrit en
    Août 2006
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Belgique

    Informations professionnelles :
    Activité : Chef programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 073
    Points : 7 977
    Points
    7 977
    Par défaut
    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.
    (Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)

    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

  6. #6
    Nouveau membre du Club
    Homme Profil pro
    Etudiant en informatique
    Inscrit en
    Juillet 2013
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Etudiant en informatique

    Informations forums :
    Inscription : Juillet 2013
    Messages : 102
    Points : 34
    Points
    34
    Par défaut
    Citation Envoyé par wax78 Voir le message
    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
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
       TarOutputStream out = new TarOutputStream( new BufferedOutputStream( dest ) );
    Malgré le fait d'avoir importé java.util.zip.*

    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

  7. #7
    Nouveau membre du Club
    Homme Profil pro
    Etudiant en informatique
    Inscrit en
    Juillet 2013
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Etudiant en informatique

    Informations forums :
    Inscription : Juillet 2013
    Messages : 102
    Points : 34
    Points
    34
    Par défaut
    J'ai adapté un nouveau code pour créer un tar :
    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
     
    //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();
    	    }
    	 }
    }
    Voici ce qui doit être dans le tar
    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
     
     
    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);
    	}
    }
    et pour finir le message d'erreur
    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
     
    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éé !
    Merci encore pour votre aide...

  8. #8
    Nouveau membre du Club
    Homme Profil pro
    Etudiant en informatique
    Inscrit en
    Juillet 2013
    Messages
    102
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Etudiant en informatique

    Informations forums :
    Inscription : Juillet 2013
    Messages : 102
    Points : 34
    Points
    34
    Par défaut
    Bonjour,

    Voici la class Tar
    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
     
    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();
    		}
    	}
    }
    il me renvoi ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    cmd /C D:\Users\T0163126\Documents\Mes Outils Personnels\WBin\\tar.exe -cf monTar.tar approbateur.txt
    mais rien ne se passe, quelqu'un serait il pourquoi ?

  9. #9
    Modérateur
    Avatar de wax78
    Homme Profil pro
    Chef programmeur
    Inscrit en
    Août 2006
    Messages
    4 073
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : Belgique

    Informations professionnelles :
    Activité : Chef programmeur
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Août 2006
    Messages : 4 073
    Points : 7 977
    Points
    7 977
    Par défaut
    Executer a la main, dans une console cette commande raconte quoi ?
    (Les "ça ne marche pas", même écrits sans faute(s), vous porteront discrédit ad vitam æternam et malheur pendant 7 ans)

    N'oubliez pas de consulter les FAQ Java et les cours et tutoriels Java

Discussions similaires

  1. Comment créer un fichier texte?
    Par Crazynoss dans le forum ASP
    Réponses: 2
    Dernier message: 08/05/2005, 19h53
  2. Réponses: 22
    Dernier message: 24/04/2005, 16h49
  3. [C#] [XML] Comment créer un fichier XML
    Par pc152 dans le forum Windows Forms
    Réponses: 12
    Dernier message: 28/09/2004, 13h41
  4. Comment créer un fichier dummy de taille donnée ?
    Par Maitre Kanter dans le forum Langage
    Réponses: 3
    Dernier message: 13/12/2003, 23h46
  5. [] [Stratégie] Comment créer un fichier log
    Par Skeezo dans le forum Installation, Déploiement et Sécurité
    Réponses: 4
    Dernier message: 16/09/2002, 18h30

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo