Bonjour,

Je galère à faire, non pas un programme qui marche, mais un programme "universel" qui sait manger n'importe quel fichier .tar.gz (réaliser par n'importe quel source) pour donner les fichiers finaux en sortie.
Un fichier .tar.gz doit pouvoir être décompressé quel que soit la source : en particulier il doit être compatible avec des archives créées par :
- linux
- 7-zip
- Programme java (compression)

Je suis arrivé à faire un programme qui marche, mais que pour certaines sources d'entrée (pas les 3 citées ci-dessus), pourriez-vous me donner un coup de main pour avoir un programme universel ?

Merci

NB : j'utilise commons compress comme lib.
Voici des exemples qui marchent (mais pas universel pour mon cas, seulement les archives faites sous linux marchent) :


// Récupération du .tar dans l'archive .tar.gz initiale
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
 private static File tarGzip2Tar(String archivePath, String archiveBaseName) throws IOException
    {
        InputStream is = new FileInputStream(new File(archivePath, archiveBaseName));
        File tarFile = null;
        BufferedInputStream bfs = new BufferedInputStream(is);
        tarFile = new File(archivePath, FilenameUtils.removeExtension(archiveBaseName));
        FileOutputStream out = new FileOutputStream(tarFile);
        org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream gzIn = new org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream(bfs);
        final byte[] buffer = new byte[1024];
        int n = 0;
        while (-1 != (n = gzIn.read(buffer)))
        {
            out.write(buffer, 0, n);
        }
        out.close();
        gzIn.close();
 
        return tarFile;
    }

//récupération des fichiers 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
private static List<File> uncompressTarGZ(File tarFile, File dest) throws IOException
    {
        dest.mkdir();
        TarArchiveInputStream tarIn = null;
        List<File> extractedFiles = new ArrayList<File>();
 
        tarIn = new TarArchiveInputStream(
                        new GzipCompressorInputStream(
                                        new BufferedInputStream(
                                                        new FileInputStream(
                                                                        tarFile
                                                        )
                                        )
                        )
                        );
 
        TarArchiveEntry tarEntry = tarIn.getNextTarEntry();
        // tarIn is a TarArchiveInputStream
        while (tarEntry != null)
        {// create a file with the same name as the tarEntry
            File destPath = new File(dest, tarEntry.getName());
            System.out.println("working: " + destPath.getCanonicalPath());
            if (tarEntry.isDirectory())
            {
                destPath.mkdirs();
            }
            else
            {
                destPath.createNewFile();
                //byte [] btoRead = new byte[(int)tarEntry.getSize()];
                byte[] btoRead = new byte[1024];
                //FileInputStream fin 
                //  = new FileInputStream(destPath.getCanonicalPath());
                BufferedOutputStream bout =
                                new BufferedOutputStream(new FileOutputStream(destPath));
                int len = 0;
 
                while ((len = tarIn.read(btoRead)) != -1)
                {
                    bout.write(btoRead, 0, len);
                }
 
                extractedFiles.add(destPath);
                bout.close();
                btoRead = null;
 
            }
            tarEntry = tarIn.getNextTarEntry();
        }
        tarIn.close();
 
        return extractedFiles;
    }
//et le programme de compression

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
/**
     * Compress (tar.gz) the input files to the output file
     *
     * @param files The files to compress
     * @param output The resulting output file (should end in .tar.gz)
     * @throws IOException
     */
    public static File compressFilesInTarGz(Collection<File> files, String absoluteDestDirname, String outputTarGzFilename)
                    throws IOException
    {
        String absoluteOutputTarGzFilename = absoluteDestDirname + File.separator + outputTarGzFilename;
        if (log.isDebugEnabled())
        {
            log.debug("Compressing " + files.size() + " to " + absoluteOutputTarGzFilename + ": " + files);
        }
        new File(absoluteDestDirname).mkdirs();
        File outputTarGzFile = new File(absoluteOutputTarGzFilename);
        outputTarGzFile.createNewFile();
        // Create the output stream for the output file
        FileOutputStream fos = new FileOutputStream(outputTarGzFile);
        // Wrap the output file stream in streams that will tar and gzip everything
        TarArchiveOutputStream taos = new TarArchiveOutputStream(
                        new GZIPOutputStream(new BufferedOutputStream(fos)));
        // TAR originally didn't support long file names, so enable the support for it
        taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
 
        // Get to putting all the files in the compressed output file
        int extensionSeparatorIndex = outputTarGzFile.getName().indexOf(FilenameUtils.EXTENSION_SEPARATOR_STR);
        String baseFilenameWithoutExtension = outputTarGzFile.getName();
        if (extensionSeparatorIndex > 0)
        {
            baseFilenameWithoutExtension = StringUtils.substring(outputTarGzFilename, 0, extensionSeparatorIndex);
        }
        for (File f : files)
        {
            addFilesToCompression(taos, f, baseFilenameWithoutExtension);
        }
 
        // Close everything up
        taos.close();
        fos.close();
 
        return outputTarGzFile;
    }
 
    /**
     * Does the work of compression and going recursive for nested directories
     * <p/>
     *
     * Borrowed heavily from http://www.thoughtspark.org/node/53
     *
     * @param taos The archive
     * @param file The file to add to the archive
         * @param dir The directory that should serve as the parent directory in the archivew
     * @throws IOException
     */
    private static void addFilesToCompression(TarArchiveOutputStream taos, File file, String dir)
                    throws IOException
    {
        // Create an entry for the file
        taos.putArchiveEntry(new TarArchiveEntry(file, file.getName()));
        if (file.isFile())
        {
            // Add the file to the archive
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            IOUtils.copy(bis, taos);
            taos.closeArchiveEntry();
            bis.close();
        }
        else if (file.isDirectory())
        {
            // close the archive entry
            taos.closeArchiveEntry();
            // go through all the files in the directory and using recursion, add them to the archive
            for (File childFile : file.listFiles())
            {
                addFilesToCompression(taos, childFile, file.getName());
            }
        }
    }