msec.org.TarUtil.java Source code

Java tutorial

Introduction

Here is the source code for msec.org.TarUtil.java

Source

/**
 * Tencent is pleased to support the open source community by making MSEC available.
 *
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the GNU General Public License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. You may 
 * obtain a copy of the License at
 *
 *     https://opensource.org/licenses/GPL-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the 
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
 */

package msec.org;

/**
 * Created by Administrator on 2016/2/16.
 */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;

//.tar
public abstract class TarUtil {

    private static final int BUFFERSZ = 1024;

    //srcPath?tar?destPath
    // ???
    public static void archive(String srcPath, String destPath) throws Exception {

        File srcFile = new File(srcPath);
        File destFile = new File(destPath);

        archive(srcFile, destFile);

    }

    public static void archive(File srcFile, File destFile) throws Exception {

        TarArchiveOutputStream taos = new TarArchiveOutputStream(new FileOutputStream(destFile));
        taos.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);

        archive(srcFile, taos, "");

        taos.flush();
        taos.close();
    }

    private static void archive(File srcFile, TarArchiveOutputStream taos, String basePath) throws Exception {
        if (srcFile.isDirectory()) {
            archiveDir(srcFile, taos, basePath);
        } else {
            archiveFile(srcFile, taos, basePath);
        }
    }

    private static void archiveDir(File dir, TarArchiveOutputStream taos, String basePath) throws Exception {

        File[] files = dir.listFiles();

        if (files.length < 1) {
            TarArchiveEntry entry = new TarArchiveEntry(basePath + dir.getName() + File.separator);

            taos.putArchiveEntry(entry);
            taos.closeArchiveEntry();
        }

        for (File file : files) {

            // 
            archive(file, taos, basePath + dir.getName() + File.separator);

        }
    }

    private static void archiveFile(File file, TarArchiveOutputStream taos, String dir) throws Exception {

        TarArchiveEntry entry = new TarArchiveEntry(dir + file.getName());

        entry.setSize(file.length());

        taos.putArchiveEntry(entry);

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        int count;
        byte data[] = new byte[BUFFERSZ];
        while ((count = bis.read(data, 0, BUFFERSZ)) != -1) {
            taos.write(data, 0, count);
        }

        bis.close();

        taos.closeArchiveEntry();
    }

    //tarsrcFile?
    public static void dearchive(File srcFile) throws Exception {
        String basePath = srcFile.getParent();
        dearchive(srcFile, new File(basePath));
    }

    public static void dearchive(File srcFile, File destFile) throws Exception {

        TarArchiveInputStream tais = new TarArchiveInputStream(new FileInputStream(srcFile));
        dearchive(destFile, tais);

        tais.close();

    }

    private static void dearchive(File destFile, TarArchiveInputStream tais) throws Exception {

        TarArchiveEntry entry = null;
        while ((entry = tais.getNextTarEntry()) != null) {

            // 
            String dir = destFile.getPath() + File.separator + entry.getName();

            File dirFile = new File(dir);

            // 
            fileProber(dirFile);

            if (entry.isDirectory()) {
                dirFile.mkdirs();
            } else {
                dearchiveFile(dirFile, tais);
            }

        }
    }

    public static void dearchive(String srcPath) throws Exception {
        File srcFile = new File(srcPath);

        dearchive(srcFile);
    }

    private static void dearchiveFile(File destFile, TarArchiveInputStream tais) throws Exception {

        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));

        int count;
        byte data[] = new byte[BUFFERSZ];
        while ((count = tais.read(data, 0, BUFFERSZ)) != -1) {
            bos.write(data, 0, count);
        }

        bos.close();
    }

    private static void fileProber(File dirFile) {

        File parentFile = dirFile.getParentFile();
        if (!parentFile.exists()) {

            // 
            fileProber(parentFile);

            parentFile.mkdir();
        }

    }

}