nay*_*kam 1 unix zip java gzip gunzip
可能的重复:
你如何用枪压缩文件并保留 .gz 文件?
我使用以下命令解压缩文件:
gunzip -f test.TGZ
Run Code Online (Sandbox Code Playgroud)
这给了我 test.tar 文件,但我丢失了 test.TGZ 文件。有没有办法保留原始文件?
编辑和更新:我通过 Java 程序调用解压缩命令。TGZ 文件至少包含 1 个图像文件、1 个文本文件和 1 个视频文件。
Java方法:执行命令
private static InputStream executeCommand(String command, File workingDir)
throws Exception {
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command, null, workingDir);
int exitValue = -1;
try {
exitValue = process.waitFor();
} catch (InterruptedException ignore) {
}
if (exitValue != 0) {
InputStream errStream = process.getErrorStream();
String errMessage = null;
if (errStream.available() > 0) {
byte[] errOutput = new byte[errStream.available()];
errStream.read(errOutput);
errMessage = new String(errOutput);
}
throw new Exception(
"Error in ExtractTGZFileTest.executeCommand(command=\""
+ command + "\") - " + errMessage);
}
return process.getInputStream();
}
public static void main(String[] args) {
try {
if (args.length == 2 && args[0].equalsIgnoreCase("tgz")) {
String archiveName = args[1];
String tarFilnme = archiveName.substring(0, archiveName
.length()
- ".tgz".length())
+ ".tar";
String gzipCommand = "gzip -c -d " + archiveName + " > "
+ tarFilnme;
InputStream is = ExtractTGZFileTest.executeCommand(gzipCommand,
null);
} else if (args.length == 2 && args[0].equalsIgnoreCase("tgz1")) {
String archiveName = args[1];
String gzipCommand = "gzip --decompress --name --verbose "
+ archiveName;
InputStream is = ExtractTGZFileTest.executeCommand(gzipCommand,
null);
} else {
System.err.println("Usage: command <file1> ");
System.exit(1);
}
System.out.println("DONE");
} catch (Exception ex) {
ex.printStackTrace();
System.exit(2);
}
}
Run Code Online (Sandbox Code Playgroud)
解决方案:目前,我将 TGZ 文件复制到临时位置并使用该文件。
Jon*_*ler 12
用:
gzip -c -d test.tgz > test.tar
Run Code Online (Sandbox Code Playgroud)
'-c' 选项表示写入标准输出(而不是修改输入文件);“-d”选项表示“解压缩”。
您还可以使用:
gunzip -c test.tgz > test.tar
Run Code Online (Sandbox Code Playgroud)
如果您有足够现代的 GNU 'tar' 版本,您可以简单地使用:
tar -xf test.tgz
Run Code Online (Sandbox Code Playgroud)
如果您的版本稍旧,则需要指定压缩程序:
tar -xzf test.tgz
Run Code Online (Sandbox Code Playgroud)
在这些版本上,您也可以使用“bzip2”:
tar -xf test.tar.bz2 --use-compress-program=bzip2
Run Code Online (Sandbox Code Playgroud)
(在更现代的版本中,选项“-j”可用于创建 bzip2 压缩的 tar 文件;解包代码会自动确定正确的解压缩程序。)
| 归档时间: |
|
| 查看次数: |
4571 次 |
| 最近记录: |