有没有办法以tar文件格式读取文件的时间戳,以便可以为untarred文件设置相同的时间。
例如: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);
}
}
答案 0 :(得分:0)
问题不明确,但要将.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