别人发给我一串代码,我需要压缩成tar文件,我需要怎么做,请求支援
在Linux系统上,把当前目录生成压缩包,在控制台里输入下面指令:
tar -zcvf test.tar.gz ./
window上,安装个git,同时把Linux基本指令也附带安装,找到要压缩的目录,右键-》"git bash here",
在窗口里执行同样的指令
tar -zcvf test.tar.gz ./
就能将当前目录压缩成test.tar.gz
要用什么语言呢?
linux命令?
window CMD命令?
还是其他脚本语言呢?
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.21</version>
</dependency>
package com.wenshi.common.util.file;
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.List;
/**
* @author valley
* @date 2022/6/22
* @Description 文件打包工具类
*/
public class FilePackUtils {
/**
* 打包tar压缩文件
*
* @param sources 文件源
* @param target 返回压缩的文件
* @return
*/
public static File pack(File[] sources, File target) {
FileOutputStream out = null;
try {
out = new FileOutputStream(target);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
TarArchiveOutputStream os = new TarArchiveOutputStream(out);
for (File file : sources) {
try {
TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file);
tarArchiveEntry.setName(file.getName());
os.putArchiveEntry(tarArchiveEntry);
IOUtils.copy(new FileInputStream(file), os);
os.closeArchiveEntry();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return target;
}
/**
* 打包tar压缩文件
*
* @param filePaths 文件源路径
* @param target 返回压缩的文件
* @return
*/
public static File pack(List<String> filePaths, File target) {
FileOutputStream out = null;
try {
out = new FileOutputStream(target);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
TarArchiveOutputStream os = new TarArchiveOutputStream(out);
for (int i = 0; i < filePaths.size(); i++) {
File file = new File(filePaths.get(i));
try {
TarArchiveEntry tarArchiveEntry = new TarArchiveEntry(file);
tarArchiveEntry.setName(file.getName());
os.putArchiveEntry(tarArchiveEntry);
IOUtils.copy(new FileInputStream(file), os);
os.closeArchiveEntry();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.flush();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return target;
}
}
如有帮助,谢谢采纳~
下载个压缩工具压缩