簡體   English   中英

如何在 Java 中創建 a.tar.gz 文件,保留相同的文件夾/目錄結構

[英]How to create a .tar.gz file, RETAINING the same folder/directory structure, in Java

我正在尋找創建以下文件夾/目錄結構的.tar.bz文件,同時保留相同的文件夾/目錄結構

ParentDir\
    ChildDir1\
        -file1
    ChildDir2\
        -file2
        -file3
    ChildDir3\
        -file4
        -file5

但是,我只能創建所有文件的 a.tar.bz,而沒有文件夾/目錄結構。 即:ParentDir.tar.bz

    ParentDir\
        -file1
        -file2
        -file3
        -file4
        -file5

使用Compress 目錄中用戶接受的答案到 tar.gz 和 Commons Compress ,我有以下代碼:

public void exportStaticFilesTar(String appID) throws, Exception {
        
        FileOutputStream fOut = null;
        BufferedOutputStream bOut = null;
        GzipCompressorOutputStream gzOut = null;
        TarArchiveOutputStream tOut = null;

        try {
            String filename = "ParentDir.tar.bz"

            //"parent/childDirToCompress/"
            String path = "<path to ParentDir>";

            fOut = new FileOutputStream(new File(filename));
            bOut = new BufferedOutputStream(fOut);
            gzOut = new GzipCompressorOutputStream(bOut);
            tOut = new TarArchiveOutputStream(gzOut);
            
            addFileToTarGz(tOut, path, "");
        } catch (Exception e) {
            log.error("Error creating .tar.gz: " +e);
        } finally {
            tOut.finish();
            tOut.close();
            gzOut.close();
            bOut.close();
            fOut.close();
        }
        
        //Download the file locally
        
    }

    private void addFileToTarGz(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.putArchiveEntry(tarEntry);
        if (f.isFile()) {
            IOUtils.copy(new FileInputStream(f), tOut);
            tOut.closeArchiveEntry();
        } else {
            tOut.closeArchiveEntry();
            File[] children = f.listFiles();
            if (children != null) {
                for (File child : children) {
                    addFileToTarGz(tOut, child.getAbsolutePath(), entryName + "/");
                }
            }
        }
    }

有人可以建議,我如何修改當前代碼以在創建 a.tar.bz 文件時保留文件夾/目錄結構。 謝謝!

我在以下方面做得更好。 重要提示:您需要將正確的初始值作為base值傳遞,即樹中的第一個目錄(不包括 fs 根目錄):

    private void addFileToTarGz(TarArchiveOutputStream tOut, String path, String base) throws IOException {
        File f = new File(path);
        String entryName = base + File.separatorChar + f.getName();
        TarArchiveEntry tarEntry = new TarArchiveEntry(f, entryName);
        tOut.putArchiveEntry(tarEntry);
        if (f.isFile()) {
            IOUtils.copy(new FileInputStream(f), tOut);
            tOut.closeArchiveEntry();
        } else {
            tOut.closeArchiveEntry();
            File[] children = f.listFiles();
            if (children != null) {
                for (File child : children) {
                    addFileToTarGz(tOut, child.getAbsolutePath(), entryName);
                }
            }
        }
    }

使用請求中提到的代碼,我能夠在我的 linux 系統上成功生成壓縮文件。

您可以通過更改用戶路徑和需要壓縮的根文件夾來復制。

public static void createTarGZ() throws FileNotFoundException, IOException
    {
        String sourceDirectoryPath = null;
        String destinationTarGzPath = null;
        FileOutputStream fileOutputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        GzipCompressorOutputStream gzOutpuStream = null;
        TarArchiveOutputStream tarArchiveOutputStream = null;

        String basePath = "/home/saad/CompressionTest/";

        try
        {

            sourceDirectoryPath = basePath + "asterisk";
            destinationTarGzPath = basePath + "asterisk.tar.gz";

            fileOutputStream = new FileOutputStream(new File(destinationTarGzPath));
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
            gzOutpuStream = new GzipCompressorOutputStream(bufferedOutputStream);
            tarArchiveOutputStream = new TarArchiveOutputStream(gzOutpuStream);

            addFileToTarGz(tarArchiveOutputStream, sourceDirectoryPath, "");
        }
        finally
        {
            tarArchiveOutputStream.finish();
            tarArchiveOutputStream.close();
            gzOutpuStream.close();
            bufferedOutputStream.close();
            fileOutputStream.close();
        }
    }

    private static void addFileToTarGz(TarArchiveOutputStream tarOutputStream, String path, String base) throws IOException
    {
        File fileToCompress = new File(path);

        String entryName = base + fileToCompress.getName();

        TarArchiveEntry tarEntry = new TarArchiveEntry(fileToCompress, entryName);

        tarOutputStream.putArchiveEntry(tarEntry);

        // If its a file, simply add it
        if (fileToCompress.isFile())
        {
            IOUtils.copy(new FileInputStream(fileToCompress), tarOutputStream);
            tarOutputStream.closeArchiveEntry();
        }
        // If its a folder then add all its contents
        else
        {
            tarOutputStream.closeArchiveEntry();
            File[] children = fileToCompress.listFiles();

            if (children != null)
            {
                // add every file/folder recursively to the tar
                for (File child : children)
                {
                    addFileToTarGz(tarOutputStream, child.getAbsolutePath(), entryName + "/");
                }
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM