解压zip文件时遇到嵌套的zip文件会继续解压。
以下代码是将压缩文件解压到本地磁盘,然后再遍历本地磁盘文件进行上传到云服务,上传完再进行删除本地磁盘文件。
压缩文件并保存在本地磁盘临时目录下:
/**
* 解压文件
* @param zipFile
*/
public void decompresComponentFile(ZipFile zipFile, String destDirPath) {
try {
File destDir = new File(destDirPath);
if (!destDir.exists()) {
destDir.mkdir();
}
ZipEntry entry;
String entryName;
File entryFile, entryDir;
byte[] buffer = new byte[1024];
int bytesRead;
for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements(); ) {
entry = (ZipEntry) e.nextElement();
entryName = entry.getName();
entryFile = new File(destDir, entryName);
if (entry.isDirectory()) {
entryFile.mkdirs();
} else {
entryDir = new File(entryFile.getParent());
if (!entryDir.exists()) {
entryDir.mkdirs();
}
try (InputStream inStream = zipFile.getInputStream(entry);
OutputStream outStream = new FileOutputStream(entryFile)){
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
}
if (entryName.endsWith(".zip")) {
decompresComponentFile(new ZipFile(entryFile.getAbsolutePath()), entryDir.getAbsolutePath());
}
}
}
zipFile.close();
} catch (IOException ex) {
try {
zipFile.close();
deleteFile(destDirPath);
} catch (IOException e) {
throw new ServiceException("解压文件失败", ex);
}
throw new ServiceException("解压文件失败", ex);
}
} |
遍历文件进行上传再删除:
/**
* 上传并删除文件,只上传文件,不上传zip包
* @param basePath
* @return
*/
public List<String> uploadAndDelete(String basePath){
List<String> downPathList = new ArrayList<>();
try {
Files.walkFileTree(Paths.get(basePath), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
String filePath = file.toString().replace(basePath.replace("/",File.separator), "");
if (!filePath.endsWith(".zip")) {
filePath = PREFIX_FOLDER.replace("/",File.separator) + filePath;
//上传文件
upLoadFile(Files.newInputStream(file), filePath, file.toFile().length());
//返回文件下载路劲
UriComponentsBuilder builder = UriComponentsBuilder.fromPath(downPath);
MediaTypeFactory.getMediaType(filePath)
.ifPresent(mediaType -> builder.queryParam("type", mediaType.toString()));
builder.queryParam("macroPath", FilenameUtils.separatorsToUnix(macroPath + filePath));
downPathList.add(builder.toUriString());
}
Files.delete(file);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
deleteFile(basePath);
throw new ServiceException(String.format("{%s}路劲下的文件上传失败;{%s}", TEMP_PATH, e.getMessage()));
}
return downPathList;
} |
遍历删除文件:
private void deleteFile(String basePath){ try { Files.walkFileTree(Paths.get(basePath), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { Files.delete(file); return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { Files.delete(dir); return FileVisitResult.CONTINUE; } }); } catch (IOException e) { throw new ServiceException("删除文件失败", e); } } } |