该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
问题补充:如XXX.tar.gz文件中的目录是【aa/中文/中文_2014_12_18_18:54:30.txt】,请问在Java里面该如何解压该文件,求大神帮忙,先谢了!!!
/**
* 解压tar.gz 文件
* @param file 要解压的tar.gz文件对象
* @param outputDir 要解压到某个指定的目录下
* @throws UnsupportedEncodingException
* @throws IOException
*/
public void unTarGz(String fileNamePath, String outputDir) throws UnsupportedEncodingException {
File file = new File(fileNamePath);
TarInputStream tarIn = null;
try {
tarIn = new TarInputStream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))), 8192);
createDirectory(outputDir, null);// 创建输出目录
TarEntry entry = null;
while ((entry = tarIn.getNextEntry()) != null) {
if (entry.isDirectory()) {// 是目录
createDirectory(outputDir, entry.getName());// 创建空目录
} else {// 是文件
File tmpFile = new File(outputDir + "/" + entry.getName());
createDirectory(tmpFile.getParent() + "/", null);// 创建输出目录
OutputStream out = null;
try {
out = new FileOutputStream(tmpFile);
int length = 0;
byte[] b = new byte[2048];
while ((length = tarIn.read(b)) != -1) {
out.write(b, 0, length);
}
} catch (IOException ex) {
throw ex;
} finally {
if (out != null) {
out.close();
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (tarIn != null) {
tarIn.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 构建目录
* @param outputDir
* @param subDir
*/
public void createDirectory(String outputDir, String subDir) {
File file = new File(outputDir);
if (!(subDir == null || subDir.trim().equals(""))) {// 子目录不为空
file = new File(outputDir + "/" + subDir);
}
if (!file.exists()) {
file.mkdirs();
}
}