Добавляйте файлы и реперториумы в архив Tar с библиотекой Apache Commons Compress, не зависящим от машины способом.

Я работаю над приложением, которому нужно создать tar-архив, чтобы вычислить его хэш. Но я сталкиваюсь с некоторыми проблемами:

  • tar не одинаков на разных машинах, тогда вычисленный хеш отличается
  • Я не могу правильно добавлять каталоги
  • Если я добавлю zip-файл в конце в tar, у меня будет содержимое из моего zip-файла:/

Я прочитал другой пост в SO и специальный учебник по apache, а также исходный тестовый код сжатой банки apache commons, но я не нашел правильного решения.

Есть ли кто-нибудь, кто может найти, где мой код неверен?

    public static File createTarFile(File[] files, File repository) {
    File tarFile = new File(TEMP_DIR + File.separator + repository.getName() + Constants.TAR_EXTENSION);
    if (tarFile.exists()) {
        tarFile.delete();
    }

    try {
        OutputStream out = new FileOutputStream(tarFile);

        TarArchiveOutputStream aos = (TarArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream("tar", out);

        for(File file : files){
            Utilities.addFileToTar(aos, file, "");
        }

        aos.finish();
        aos.close();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return tarFile;
}

private static void addFileToTar(TarArchiveOutputStream tOut, File file, String base) throws IOException {

    TarArchiveEntry entry = new TarArchiveEntry(file, base + file.getName());
    entry.setModTime(0);
    entry.setSize(file.length());
    entry.setUserId(0);
    entry.setGroupId(0);
    entry.setUserName("avalon");
    entry.setGroupName("excalibur");
    entry.setMode(0100000);
    entry.setSize(file.length());
    tOut.putArchiveEntry(entry);

    if (file.isFile()) {
        IOUtils.copy(new FileInputStream(file), tOut);
        tOut.closeArchiveEntry();
    } else {
        tOut.closeArchiveEntry();
        File[] children = file.listFiles();
        if (children != null) {
            for (File child : children) {
                addFileToTar(tOut, child, file.getName());
            }
        }
    }
}

Спасибо.


person MonkeyJLuffy    schedule 03.12.2015    source источник
comment
Чем отличаются архивы? Я мог предположить, что порядок записей отличается, поскольку listFiles может возвращать файлы в другом порядке. Или это может быть кодировка имен файлов. Вы не должны устанавливать размер, конструктор TarEntry уже делает это (это может быть причиной сбоя добавления каталогов, length вполне может вернуть › 0 для каталогов). Что, кроме добавления содержимого zip-архива, вы ожидаете при добавлении zip-файла?   -  person Stefan Bodewig    schedule 03.12.2015


Ответы (1)


Я наконец нашел решение после прочтения сообщения caarlos0: Проблема кодирования при сжатии файлов с помощью Apache Commons Compression в Linux

Используя библиотеку apache-commons-1.8.jar, я создал класс инструментов, который может выполнять эту работу:

Вы можете найти этот код здесь: GitHub-репозиторий библиотеки MakeTar

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;

/**
 * The Class TarArchive.
 */
public class TarArchive {

    /**
     * Creates the tar of files.
     *
     * @param files the files
     * @param tarPath the tar path
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static void createTarOfFiles(String[] files, String tarPath) throws IOException
    {
        FileOutputStream fOut = null;
        BufferedOutputStream bOut = null;
        TarArchiveOutputStream tOut = null;

        Arrays.sort(files);
        try
        {
            fOut = new FileOutputStream(new File(tarPath));
            bOut = new BufferedOutputStream(fOut);
            tOut = new TarArchiveOutputStream(bOut);

            for (String file : files) {
                addFileToTar(tOut, file, "");
            }
        }
        finally
        {
            tOut.finish();
            tOut.close();
            bOut.close();
            fOut.close();
        }
    }

    /**
     * Creates the tar of directory.
     *
     * @param directoryPath the directory path
     * @param tarPath the tar path
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static void createTarOfDirectory(String directoryPath, String tarPath) throws IOException
    {
        FileOutputStream fOut = null;
        BufferedOutputStream bOut = null;
        TarArchiveOutputStream tOut = null;

        try
        {
            fOut = new FileOutputStream(new File(tarPath));
            bOut = new BufferedOutputStream(fOut);
            tOut = new TarArchiveOutputStream(bOut);

            addFileToTar(tOut, directoryPath, "");
        }
        finally
        {
            tOut.finish();
            tOut.close();
            bOut.close();
            fOut.close();
        }
    }

    /**
     * Adds the file to tar.
     *
     * @param tOut the t out
     * @param path the path
     * @param base the base
     * @throws IOException Signals that an I/O exception has occurred.
     */
    private static void addFileToTar(TarArchiveOutputStream tOut, String path, String base) throws IOException
    {
        File f = new File(path);
        String entryName = base + f.getName();
        TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);

        tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

        if(f.isFile())
        {
           tarEntry.setModTime(0);
           tOut.putArchiveEntry(tarEntry);

           IOUtils.copy(new FileInputStream(f), tOut);

           tOut.closeArchiveEntry();
        }
        else
        {
            File[] children = f.listFiles();
            Arrays.sort(children);

            if(children != null)
            {
                for(File child : children)
                {
                    addFileToTar(tOut, child.getAbsolutePath(), entryName + "/");
                }
            }
        }
    }
}

Спасибо, что прочитали меня.

РЕДАКТИРОВАТЬ: Небольшая коррекция, я добавил сортировку массивов.

РЕДАКТИРОВАТЬ 2: я исправил код, чтобы иметь один и тот же архив на всей машине. Хэш, рассчитанный на архиве, везде одинаков.

person MonkeyJLuffy    schedule 09.12.2015