`

java.io.IOException: Error detected parsing the header

    博客分类:
  • JAVA
阅读更多

今天在用commons-compress-1.9.jar  做tar解压时在while((entry = tar.getNextEntry())!=null){  出发生异常。异常内容:java.io.IOException: Error detected parsing the header和cause by:java.lang.IllegalArgumentException: Invalid byte 46 at offset 2 in '01.pdf{NUL}�' len=8

检查原因是由于tar文件并不是真正的在linux环境中生成的,而是在windows生成的,所以要处理这个问题,只需要在linux重新tar一次即可。

这里做个记录,以免忘记

public boolean deArchiveFile(File sourcefile,String destPath){
		TarArchiveInputStream tar = null;
		TarArchiveEntry entry = null;
		BufferedOutputStream bos = null;
		try {
			tar = new TarArchiveInputStream(new FileInputStream(sourcefile));
			File destFileDir = new File(destPath);
			if(!destFileDir.exists()){
				destFileDir.mkdir();
			}
			while((entry = tar.getNextTarEntry())!=null){
				if(entry.isDirectory()){
					if(mkNewDir(destPath,entry.getName())){
//						destPath=destPath+File.separator+entry.getName();
					}
				}else{
					try{
						byte b[] = new byte[BUFFER_MAX_SIZE];
						bos = new BufferedOutputStream(new FileOutputStream(destPath+File.separator+entry.getName()),BUFFER_MAX_SIZE);
						int count = 0;
						while((count = tar.read(b, 0, BUFFER_MAX_SIZE))!=-1){
							bos.write(b, 0, count);
						}
					}finally{
						bos.flush();
						bos.close();
					}
					
				}
			}
			return true;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			if(bos!=null){
				try {
					bos.flush();
					bos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return false;
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics