public static File toTarGz()

in tomee-patch-core/src/main/java/org/apache/tomee/patch/core/ZipToTar.java [33:80]


    public static File toTarGz(final File zip) throws Exception {

        final String tarGzName = zip.getName().replaceAll("\\.(zip|jar)$", ".tar.gz");
        final File tarGz = new File(zip.getParentFile(), tarGzName);

        try (final InputStream in = IO.read(zip); final TarArchiveOutputStream tarOut = tar(tarGz)) {

            tarOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);

            final ZipInputStream zipIn = new ZipInputStream(in);

            ZipEntry zipEntry;
            while ((zipEntry = zipIn.getNextEntry()) != null) {
                final String name = zipEntry.getName();

                // The ZipEntry often shows -1 as the size and the
                // TarArchiveOutputStream API requires us to know the
                // exact size before we start writing.  So we have to
                // buffer everything in memory first to learn the size
                final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
                IO.copy(zipIn, buffer);
                final byte[] bytes = buffer.toByteArray();

                final TarArchiveEntry tarEntry = new TarArchiveEntry(name);

                // Set the size and date
                tarEntry.setSize(bytes.length);
                tarEntry.setModTime(zipEntry.getLastModifiedTime().toMillis());

                // Mark any shell scripts as executable
                if (name.endsWith(".sh")) {
                    tarEntry.setMode(493);
                }

                // Finally out the Entry into the archive
                // Any attributes set on tarEntry after this
                // point are ignored.
                tarOut.putArchiveEntry(tarEntry);

                IO.copy(bytes, tarOut);
                tarOut.closeArchiveEntry();
            }

            tarOut.finish();
        }

        return tarGz;
    }