By Java file compression, packing a bag collector tar.gz

First, how to package Java files

Maven 1.1 add-dependent

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.12</version>
</dependency>

 

1.2 Packaging core code

By means packing idea is as follows:Apachecompress

  • ①: Create an FileOutputStreamoutput file (.tar.gz) files.

  • ②: create a GZIPOutputStream, used to wrap FileOutputStreamthe object.

  • ③: create a TarArchiveOutputStream, used to wrap GZIPOutputStreamthe object.

  • ④: Then, read all the files in the folder.

  • ⑤: If it is a directory, it is added to TarArchiveEntry.

  • ⑥: if the file still add it to TarArchiveEntry, and then the contents of the file needs to be written TarArchiveOutputStreamin.

 

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

import java.io.*;
import java.util.zip.GZIPOutputStream;
{class TarUtils public 
public static void the compress (sourceFolder String, String tarGzPath) throws IOException {
createTarFile (sourceFolder, tarGzPath);
}
Private static void createTarFile (sourceFolder String, String tarGzPath) {
TarArchiveOutputStream TAROS = null;
the try {
// Create a FileOutputStream to the output file (.tar.gz) FileOutputStream fos = new new FileOutputStream (tarGzPath);
// Create a GZIPOutputStream, for packaging new new FileOutputStream object as GZIPOutputStream = GOS as GZIPOutputStream (BufferedOutputStream the new new (fos));
// Create a TarArchiveOutputStream, for packaging GZIPOutputStream objects
tarOs = new TarArchiveOutputStream (gos);
// If this mode is set when the file name of more than 100 bytes will throw an exception, the exception is as follows:
// IS TOO Long (> 100 bytes)
// specifically refer to the official document: http: // commons. apache.org/proper/commons-compress/tar.html#Long_File_Names
tarOs.setLongFileMode (TarArchiveOutputStream.LONGFILE_POSIX);
addFilesToTarGZ (sourceFolder, "", TAROS);
} the catch (IOException E) {
e.printStackTrace ();
} {the finally
{the try
tarOs.close ();
} the catch (IOException E) {
e.printStackTrace ();
}
}
}

public static void addFilesToTarGZ(String filePath, String parent, TarArchiveOutputStream tarArchive) throws IOException {
File file = new File(filePath);
// Create entry name relative to parent file pat
String entryName = parent + file.getName();
// 添加 tar ArchiveEntry
tarArchive.putArchiveEntry(new TarArchiveEntry(file, entryName));
if (file.isFile()) {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
// 写入文件
IOUtils.copy(bis, tarArchive);
tarArchive.closeArchiveEntry();
bis.close();
IF the else} (file.isDirectory ()) {
// folder because, without writing the contents, can be closed
tarArchive.closeArchiveEntry ();
// read the folder and all files
for (File f: file.listFiles ( )) {
// recursive
addFilesToTarGZ (f.getAbsolutePath (), + the File.separator entryName, tarArchive);
}
}
}

public static void main (String [] args) throws IOException {
// test wave, the filebeat-7.1. 0-linux-x86_64 package name as filebeat-7.1.0-linux-x86_64.tar.gz the tar package
compress ( "/ Users / a123123 / Work / filebeat-7.1.0-linux-x86_64", "/ Users / a123123 /Work/tmp_files/filebeat-7.1.0-linux-x86_64.tar.gz ");

}
}


  

Guess you like

Origin www.cnblogs.com/kevin-ying/p/11800775.html