JAVA Zip 압축 Tar 압축 tar. gz 압축
org.apache.commons
commons-compress
1.4.1
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.apache.commons.compress.utils.IOUtils;
import org.apache.log4j.Logger;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* @author Rorschach
* @title: TestAll
* @description: TODO
* @date 2019-05-229:40
*/
public class CompressUtil {
private String zipFileName; // Zip
private String sourceFileName; // ( )
private static Logger logger = Logger.getLogger(CompressUtil.class.getName());
public CompressUtil() {
}
public CompressUtil(String zipFileName, String sourceFileName) {
this.zipFileName = zipFileName;
this.sourceFileName = sourceFileName;
}
/**
* @description: ,
* @author Rorschach
* @date 2019-05-23 15:55
*/
public void fullTar(String tempDir) throws Exception {
String path = getPath(tempDir);
File allFile = new File(path);
File[] files = allFile.listFiles();
String resultName = "";
for (File file : files) {
resultName = path + "\\" + file.getName() + ".tar";
new CompressUtil(resultName, path + "\\" + file.getName() + "\\").tar();
gzipCompression(resultName, resultName + ".gz");
}
}
/**
* @description:
* @author Rorschach
* @date 2019-05-23 15:54
*/
public String getPath(String path) {
File allFile = new File(path);
File[] files = allFile.listFiles();
String dirPath = "";
for (int i = 0; i < files.length; i++) {
logger.info(files[i].getName());
if (files[i].isDirectory() && files[i].getName().startsWith("RADA_ST_DOR_L2_CAP")) {
logger.info("true");
dirPath = files[i].getParent();
break;
} else {
String rs = getPath(files[i].getPath());
if (rs != null && !"".equals(rs)) {
return rs;
}
}
}
if (dirPath.length() != 0) {
return dirPath;
} else {
return " ";
}
}
/**
* @description: zip
* @author Rorschach
* @date 2019-05-23 15:54
*/
public void zip() throws Exception {
//File zipFile = new File(zipFileName);
logger.info(" ...");
ZipOutputStream out = null;
BufferedOutputStream bos = null;
try {
// zip
out = new ZipOutputStream(new FileOutputStream(zipFileName));
File sourceFile = new File(sourceFileName);
//
compress(out, sourceFile, sourceFile.getName());
} catch (Exception e1) {
logger.info(" !!!!!!!!! ");
e1.printStackTrace();
} finally {
if (bos != null)
bos.close();
if (out != null)
out.close();
}
System.out.println(" ");
}
/**
* @description: tar
* @author Rorschach
* @date 2019-05-23 15:53
*/
public void tar() throws Exception {
//File zipFile = new File(zipFileName);
logger.info("tar ...");
TarArchiveOutputStream out = null;
BufferedOutputStream bos = null;
try {
// tar
out = new TarArchiveOutputStream(new FileOutputStream(zipFileName), 512000);
File sourceFile = new File(sourceFileName);
//
tarCompress(out, sourceFile, sourceFile.getName());
} catch (Exception e1) {
logger.info(" !!!!!!!!! ");
e1.printStackTrace();
} finally {
if (out != null)
out.close();
if (bos != null)
bos.close();
}
System.out.println(" ");
}
/**
* @description: zip
* @author Rorschach
* @date 2019-05-23 15:53
*/
public void compress(ZipOutputStream out, File sourceFile, String base) throws Exception {
// ( )
if (sourceFile.isDirectory()) {
// ( )
File[] flist = sourceFile.listFiles();
if (flist.length == 0)// , zip
{
System.out.println(base + "/");
out.putNextEntry(new ZipEntry(base + "/"));
} else// , compress, ( )
{
for (int i = 0; i < flist.length; i++) {
compress(out, flist[i], base + "/" + flist[i].getName());
}
}
} else// ( ), , , tar
{
out.putNextEntry(new ZipEntry(base));
FileInputStream fos = new FileInputStream(sourceFile);
BufferedInputStream bis = new BufferedInputStream(fos);
int tag;
System.out.println(base);
// zip , zip , 7Z
while ((tag = bis.read()) != -1) {
out.write(tag);
}
fos.close();
out.flush();
bis.close();
}
}
/**
* @description: Tar
* @author Rorschach
* @date 2019-05-23 15:52
*/
public void tarCompress(TarArchiveOutputStream out, File sourceFile, String base) throws Exception {
InputStream inputStream;
// ( )
if (sourceFile.isDirectory()) {
// ( )
File[] flist = sourceFile.listFiles();
if (flist.length == 0)// , zip
{
System.out.println(base + "/");
out.putArchiveEntry(new TarArchiveEntry(base + "/"));
} else// , compress, ( )
{
for (int i = 0; i < flist.length; i++) {
tarCompress(out, flist[i], base + "/" + flist[i].getName());
}
}
} else// ( ), , , zip
{
out.putArchiveEntry(new TarArchiveEntry(sourceFile, sourceFile.getName()));// ,
inputStream = new FileInputStream(sourceFile);
IOUtils.copy(inputStream, out);
out.closeArchiveEntry();
}
}
/**
* @description: tar gz
* @author Rorschach
* @date 2019-05-23 15:52
*/
public static boolean gzipCompression(String filePath, String resultFilePath) throws IOException {
logger.info(" gzipCompression -> Compression start!");
InputStream fin = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
GzipCompressorOutputStream gcos = null;
try {
fin = Files.newInputStream(Paths.get(filePath));
bis = new BufferedInputStream(fin);
fos = new FileOutputStream(resultFilePath);
bos = new BufferedOutputStream(fos);
gcos = new GzipCompressorOutputStream(bos);
byte[] buffer = new byte[1024];
int read = -1;
while ((read = bis.read(buffer)) != -1) {
gcos.write(buffer, 0, read);
}
} finally {
new File(filePath).delete();
if (gcos != null)
gcos.close();
if (bos != null)
bos.close();
if (fos != null)
fos.close();
if (bis != null)
bis.close();
if (fin != null)
fin.close();
}
logger.info(" gzipCompression -> Compression end!");
return true;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.