Java在内存中将Map打包为tar.gz

背景

近期需要将一些数据数据打成tar.gz包,放到ftp上供合作方拉取。在网上查了下Java打包文件的方式, 发现很多例子都是基于文件操作进行了。在实际业务中,并不需要将文件落盘,只需放到ftp即可。为了保证效率, 打包时最好不写本地磁盘,完全在内存中进行。下面的例子,简单演示了将Map打包为tar.gz字节流。

代码Demo

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.springframework.util.CollectionUtils;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * @author wangbowen
 * @since 18-5-21
 */

public class TarUtil {
  /**
   * 将Map打包成tar.gz字节流
   *
   * @param sourceData
   * @return
   */
  public static byte[] tarGz(Mapbyte[]> sourceData) {
    System.out.println(CollectionUtils.isEmpty(sourceData));
    if (CollectionUtils.isEmpty(sourceData) == false) {
      ByteArrayOutputStream byteArrayOutputStream = null;
      BufferedOutputStream bufferedOutputStream = null;
      GzipCompressorOutputStream compressorOutputStream = null;
      TarArchiveOutputStream tarArchiveOutputStream = null;
      try {
        byteArrayOutputStream = new ByteArrayOutputStream();
        bufferedOutputStream = new BufferedOutputStream(byteArrayOutputStream);
        compressorOutputStream = new GzipCompressorOutputStream(bufferedOutputStream);
        tarArchiveOutputStream = new TarArchiveOutputStream(compressorOutputStream);
        Setbyte[]>> set = sourceData.entrySet();
        Iteratorbyte[]>> iterator = set.iterator();
        while (iterator.hasNext()) {
          Map.Entrybyte[]> entry = iterator.next();
          String fileName = entry.getKey();
          byte[] fileData = entry.getValue();
          TarArchiveEntry tarEntry = new TarArchiveEntry(fileName);
          tarEntry.setSize(fileData.length);
          tarArchiveOutputStream.putArchiveEntry(tarEntry);
          tarArchiveOutputStream.write(fileData, 0, fileData.length);
          tarArchiveOutputStream.closeArchiveEntry();
        }
        tarArchiveOutputStream.close();
        compressorOutputStream.close();
        byteArrayOutputStream.flush();
        return byteArrayOutputStream.toByteArray();
      } catch (FileNotFoundException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        try {
          if (null != byteArrayOutputStream) {
            byteArrayOutputStream.close();
          }
          if (null != bufferedOutputStream) {
            bufferedOutputStream.close();
          }
          if (null != compressorOutputStream) {
            compressorOutputStream.close();
          }
          if (null != tarArchiveOutputStream) {
            tarArchiveOutputStream.close();
          }
        } catch (IOException e) {
        }
      }
    }
    return null;
  }

}

测试

public class TarUtilTest {
  public static void main(String[] args) throws Exception {
    String name1 = "test1.txt", name2 = "test2.txt", name3 = "test3.txt";
    String file1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    String file2 = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
    String file3 = "ccccccccccccccccccccccccccccccccccccccccc";

    Mapbyte[]> sourceData = new HashMap<>();
    sourceData.put(name1, file1.getBytes());
    sourceData.put(name2, file2.getBytes());
    sourceData.put(name3, file3.getBytes());

    byte[] tarGzBytes = TarUtil.tarGz(sourceData);
    // 字节流已经可以往ftp传了,这里为了测试写一下本地文件
    FileOutputStream fos = new FileOutputStream("/home/work/test123.tar.gz");
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    bos.write(tarGzBytes);
    bos.close();
    fos.close();
  }
}

检查tar.gz压缩包,双击可以正常解压看到包内的文件,如下图。
Java在内存中将Map打包为tar.gz_第1张图片

你可能感兴趣的:(Java,tar包,Java基础)