引入jar包 junrar-2.0.0.jar
http://mvnrepository.com/artifact/com.github.junrar/junrar
问题:
junrar只支持PC上WinRAR5.0以下版本压缩生成的文件,WinRAR5.0压缩出来的文件格式其实是rar5格式,是解压不出来的。WinRAR5.0以下版本压缩生成的文件是rar4,可以解压出来。
MultipartFile multipartFile = request.getFile(it);
String extension = multipartFile.contentType;
InputStream is = multipartFile.getInputStream();
InputStream stream = new BufferedInputStream(is);
Archive archive = new Archive(stream);
for(FileHeader fh : archive.getFileHeaders()){
String fileName= fh.getFileNameW().trim();
if(!existZH(fileName)){
fileName = fh.getFileNameString().trim();
}
println fileName;
File dir = null
File out = null
FileOutputStream os = null
String path = (fileName).replaceAll("\\\\", "/");
String[] arr = path.split("/");
int length = arr.length;
String dirPath = "";
int end = path.lastIndexOf("/");
if (end != -1) {
dirPath = path.substring(0, end);
}
try {
dir = new File(dirPath);
if (!dir.exists()) {
// dir.mkdirs();
}
} catch (RuntimeException e1) {
e1.printStackTrace();
} finally {
if (dir != null) {
dir = null;
}
}
if(arr.length>2&&arr[length-2].lastIndexOf("_")>0&&!dataMap.containsKey(arr[length-2])){
dataMap.put(arr[length-2],new ArrayList<String>());
}
System.err.println("file - " + fh.getFileNameW() + " : "
+ fh.getDataSize() + " bytes");
long sizes = fh.getDataSize();
if (sizes > 0) {
if(arr[length-1].lastIndexOf("_")<0&&arr[length-2].lastIndexOf("_")<0){
InputStream dirStream = archive.getInputStream(fh)
Workbook workbook = WorkbookFactory.create(dirStream);
//具体业务数据
...
}else if(arr[length-2].lastIndexOf("_")>0){
InputStream dirStream = archive.getInputStream(fh);
byte[] bytes = dirStream.getBytes();
//具体业务数据
}
}
fh = archive.nextFileHeader();
// out = new File(fileName);
try {
// os = new FileOutputStream(out);
// archive.extractFile(fh, os);
} catch (FileNotFoundException e) {
e.printStackTrace();
}finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
out = null;
}
}
}