代码:
/**
* 解压zip文件,将制定类型的文件解压到指定路径下,支持中文名字
*
* @param in zip压缩包的 字符流
* @param dir 解压后的根目录
* @return Map 返回文件的名称和对应的路径
* @throws IOException
*/
public static Map<String, String> unZipFile(InputStream in, String dir,
String fileType) throws IOException {
Map<String, String> map = new HashMap<String, String>();
ZipArchiveInputStream inputStream = new ZipArchiveInputStream(in,"GBK", true);
File newdir = new File(dir);
if (!newdir.exists())
newdir.mkdir();
byte[] bytes = new byte[1024];
int slen;
ArchiveEntry file = inputStream.getNextEntry();
while (file != null) {
if (file.isDirectory()) {
file = inputStream.getNextEntry();
continue;
}
String fileName = file.getName();
fileName = fileName.substring(fileName.lastIndexOf("/") + 1,
fileName.length());
String type = fileName.substring(fileName.lastIndexOf('.'));
if (type.equalsIgnoreCase(fileType)) {
map.put(fileName, dir + File.separator + fileName);
FileOutputStream out = new FileOutputStream(dir+ File.separator + fileName);
while ((slen = inputStream.read(bytes, 0, bytes.length)) != -1) {
out.write(bytes, 0, slen);
}
out.close();
}
file = inputStream.getNextEntry();
}
inputStream.close();
return map;
}
现在的情况是这样的:
压缩包底下直接为压缩文件,压缩文件没有父级文件夹时无法正常工作。比如:A.ZIP下有 a.xls,b.xls,c.xls;
ArchiveEntry file = inputStream.getNextEntry();返回的file永远是空值。如果在添加一个文件件,就不会出现这种情况,比如:A.zip 下有a文件夹,a文件夹下有a.xls,b.xls,c.xls 可以正常工作.
请哪位大哥给小弟看看,谢谢!
/**
* 解压zip文件,将制定类型的文件解压到指定路径下,支持中文名字
*
* @param in zip压缩包的 字符流
* @param dir 解压后的根目录
* @return Map 返回文件的名称和对应的路径
* @throws IOException
*/
public static Map<String, String> unZipFile(InputStream in, String dir,
String fileType) throws IOException {
Map<String, String> map = new HashMap<String, String>();
ZipArchiveInputStream inputStream = new ZipArchiveInputStream(in,"GBK", true);
File newdir = new File(dir);
if (!newdir.exists())
newdir.mkdir();
byte[] bytes = new byte[1024];
int slen;
ArchiveEntry file = inputStream.getNextEntry();
while (file != null) {
if (file.isDirectory()) {
file = inputStream.getNextEntry();
continue;
}
String fileName = file.getName();
fileName = fileName.substring(fileName.lastIndexOf("/") + 1,
fileName.length());
String type = fileName.substring(fileName.lastIndexOf('.'));
if (type.equalsIgnoreCase(fileType)) {
map.put(fileName, dir + File.separator + fileName);
FileOutputStream out = new FileOutputStream(dir+ File.separator + fileName);
while ((slen = inputStream.read(bytes, 0, bytes.length)) != -1) {
out.write(bytes, 0, slen);
}
out.close();
}
file = inputStream.getNextEntry();
}
inputStream.close();
return map;
}
现在的情况是这样的:
压缩包底下直接为压缩文件,压缩文件没有父级文件夹时无法正常工作。比如:A.ZIP下有 a.xls,b.xls,c.xls;
ArchiveEntry file = inputStream.getNextEntry();返回的file永远是空值。如果在添加一个文件件,就不会出现这种情况,比如:A.zip 下有a文件夹,a文件夹下有a.xls,b.xls,c.xls 可以正常工作.
请哪位大哥给小弟看看,谢谢!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货