我似乎无法导入所需的包或找到如何提取任何网上的例子.tar.gz
的Java文件。
是什么使得它更糟糕的是我使用的JSP页面时遇到了问题导入包到我的项目。 我复制的.jar的到WebContent/WEB-INF/lib/
然后用鼠标右键单击该项目,并选择导入外部JAR和进口它。 有时,包解决,其他时间他们不这样做。 似乎无法得到GZIP导入无论是。 对于JSP Eclipse中的进口不直观,就像他们在正常的Java代码,你可以右键单击一个公认的包,然后选择导入。
我已经试过了Apache公共图书馆,冰和另外一个叫联合战术空中攻击。 冰已经进口的,但我无法找到如何使用它的例子吗?
我想我需要先解压缩gzip压缩部分,然后用tarstream打开呢?
任何帮助是极大的赞赏。
该接受的答案工作正常,但我认为这是多余的有写文件操作。
你可以使用像
TarArchiveInputStream tarInput =
new TarArchiveInputStream(new GZipInputStream(new FileInputStream("Your file name")));
TarArchiveEntry currentEntry = tarInput.getNextTarEntry();
while(currentEntry != null) {
File f = currentEntry.getFile();
// TODO write to file as usual
}
希望这有助于。
Maven的回购
好吧,我终于想通了这一点,这是我的代码的情况下,这可以帮助任何人的未来。 它用Java编写的,使用Apache公地IO和压缩图书馆的。
File dir = new File("directory/of/.tar.gz/files/here");
File listDir[] = dir.listFiles();
if (listDir.length!=0){
for (File i:listDir){
/* Warning! this will try and extract all files in the directory
if other files exist, a for loop needs to go here to check that
the file (i) is an archive file before proceeding */
if (i.isDirectory()){
break;
}
String fileName = i.toString();
String tarFileName = fileName +".tar";
FileInputStream instream= new FileInputStream(fileName);
GZIPInputStream ginstream =new GZIPInputStream(instream);
FileOutputStream outstream = new FileOutputStream(tarFileName);
byte[] buf = new byte[1024];
int len;
while ((len = ginstream.read(buf)) > 0)
{
outstream.write(buf, 0, len);
}
ginstream.close();
outstream.close();
//There should now be tar files in the directory
//extract specific files from tar
TarArchiveInputStream myTarFile=new TarArchiveInputStream(new FileInputStream(tarFileName));
TarArchiveEntry entry = null;
int offset;
FileOutputStream outputFile=null;
//read every single entry in TAR file
while ((entry = myTarFile.getNextTarEntry()) != null) {
//the following two lines remove the .tar.gz extension for the folder name
String fileName = i.getName().substring(0, i.getName().lastIndexOf('.'));
fileName = fileName.substring(0, fileName.lastIndexOf('.'));
File outputDir = new File(i.getParent() + "/" + fileName + "/" + entry.getName());
if(! outputDir.getParentFile().exists()){
outputDir.getParentFile().mkdirs();
}
//if the entry in the tar is a directory, it needs to be created, only files can be extracted
if(entry.isDirectory){
outputDir.mkdirs();
}else{
byte[] content = new byte[(int) entry.getSize()];
offset=0;
myTarFile.read(content, offset, content.length - offset);
outputFile=new FileOutputStream(outputDir);
IOUtils.write(content,outputFile);
outputFile.close();
}
}
//close and delete the tar files, leaving the original .tar.gz and the extracted folders
myTarFile.close();
File tarFile = new File(tarFileName);
tarFile.delete();
}
}