java - 在解压缩 .gz 文件时,解压缩并创建为本地副本的文件没有 .gz 可用的原始文件的最后修改时间

标签 java gzip tar apache-commons

有没有办法读取 tar 文件格式的文件时间戳,以便为解压文件设置相同的时间。

例如:Tar 文件中有多个文件,我想读取一个文件的最后修改时间戳。

请查找下面使用的代码。

我正在使用 apache commons:commons-compress-1.2.jar

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 

import org.apache.commons.compress.archivers.tar.TarArchiveEntry; 
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; 
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; 

public class UnTar { 

public static void uncompressTarGZ(File tarFile, File dest) throws IOException { 
dest.mkdir(); 
TarArchiveInputStream tarIn = null; 

tarIn = new TarArchiveInputStream( 
new GzipCompressorInputStream( 
new BufferedInputStream( 
new FileInputStream( 
tarFile 
) 
) 
) 
); 

TarArchiveEntry tarEntry = tarIn.getNextTarEntry(); 

// tarIn is a TarArchiveInputStream 
while (tarEntry != null) {// create a file with the same name as the tarEntry 
System.out.println("********"+tarEntry.getName()); 
File destPath = new File(dest, tarEntry.getName()); 
System.out.println("working: " + destPath.getCanonicalPath()); 
if (tarEntry.isDirectory()) { 
destPath.mkdirs(); 
} else { 
if(destPath.getName().endsWith(".xml") || destPath.getName().endsWith(".tp2")){ 
byte[] header = new byte[10]; 
tarIn.read(header); 
int timestamp = header[4] & 0xFF | 
(header[5] & 0xFF) << 8 | 
(header[6] & 0xFF) << 16 | 
(header[7] & 0xFF) << 24; 

destPath.setLastModified(timestamp); 
destPath.createNewFile(); 
destPath.setLastModified(tarEntry.getLastModifiedDate().getTime()); 
//byte [] btoRead = new byte[(int)tarEntry.getSize()]; 
byte [] btoRead = new byte[1024]; 
//FileInputStream fin 
// = new FileInputStream(destPath.getCanonicalPath()); 
BufferedOutputStream bout = 
new BufferedOutputStream(new FileOutputStream(destPath)); 
int len = 0; 

while((len = tarIn.read(btoRead)) != -1) 
{ 
bout.write(btoRead,0,len); 
} 

bout.close(); 
btoRead = null; 
} 
} 
tarEntry = tarIn.getNextTarEntry(); 
} 
tarIn.close(); 

} 

public static void main(String[] args) throws IOException { 
File tarFile = new File("D:/1_RealDoc_Classic/Work/RDC_927_ReconcileVendor_Util/Production_Samples/FSM_Files/ocwenamf.20150107210002.tar.gz"); 
File dest = new File("D:/1_RealDoc_Classic/Work/RDC_927_ReconcileVendor_Util/Production_Samples/FSM_Files/UnZipped"); 
uncompressTarGZ(tarFile, dest); 
} 

}

最佳答案

问题不清楚,但要将 .tar.gz 文件的修改时间设置为其中存档的文件的最新修改时间,我使用以下命令:

#!/bin/tcsh -f
# set the mod time of a .tar.gz file to the mod time of the latest file within
foreach x ( $* )
  touch -t `tar tvz --full-time -f $x |& egrep -v '^tar' | awk '{ print $4, $5 }' | sort | tail -1 | sed 's/ //g' | sed 's/-//g' | sed 's/://' | sed 's/:/./'` $x
end

关于java - 在解压缩 .gz 文件时,解压缩并创建为本地副本的文件没有 .gz 可用的原始文件的最后修改时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27898517/

相关文章:

javascript - 通过ajax从本地目录解析gz文件中的json - angularJS

linux - tar/gzip 不包括某些文件

linux - tar zxf 与 -xvf 之间的区别

tar:无法读取:是 docker 中的目录

java - Glassfish,在 glassfish/domain/domain1 中找不到文件夹 portlet-container

http - 在实时运行的 Linux 系统上监控 HTTP gzip 流量的简单方法是什么?

java - 如何在 recyclerView android 中停止项目重复

linux - 安装 PopcornTime 时找不到 .install 命令

java - Netbeans 和实体生成

java - 严重 : ContainerBase. addChild : start:org. apache.catalina.LifecycleException: 无法启动错误