rar上傳壓縮包zip,rar
/**
* 對上傳的檔案流進行處理
*
* @param file
* @param userInfo
* @return
*/
public boolean uploadVoice(MultipartFile file, UserInfo userInfo) {
List<HashMap<String, Object>> taskList = null;
String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
try {
if("zip".equals(type.toLowerCase())){
taskList = zip("GBK", file, userInfo);
}else if("rar".equals(type.toLowerCase())){
taskList = rar(file, userInfo);
}
} catch (Exception e) {
try {
LOGGER.error("[VoiceMarkServiceImpl][uploadVoice]:-------GBK編碼解壓失敗,使用UTF-8開始解壓", e);
if("zip".equals(type.toLowerCase())){
taskList = zip("UTF-8", file, userInfo);
}else if("rar".equals(type.toLowerCase())){
taskList = rar(file, userInfo);
}
} catch (Exception e2) {
LOGGER.error("[VoiceMarkServiceImpl][uploadVoice]:-------UTF-8解壓錯誤", e);
return false;
}
}
if(taskList == null){
return false;
}else{
dao.batchAdd(taskList);
return true;
}
}
/**
* 處理上傳的壓縮包,解壓獲取錄音檔案屬性
*
* @param charset
* 字元編碼
* @param zipPath
* @param uploadPath
* 上傳地址
* @return 每個錄音檔案的屬性,用來插入資料庫
* @throws Exception
*/
public List<HashMap<String, Object>> zip(String charset,
MultipartFile mfile, UserInfo userInfo) throws Exception {
LOGGER.info("[ServiceImpl][zip]:-------獲取的上傳路徑uploadPath="
+ Constants
.getAppConfigProperty("SAVEPATH"));
List<HashMap<String, Object>> fileList = new ArrayList<HashMap<String, Object>>();
// 解決zip檔案中有中文目錄或者中文檔案
// ZipFile zip=new ZipFile(zipFile,Charset.forName(charset));//輸入源zip路徑
try (ZipInputStream zip = new ZipInputStream(mfile.getInputStream(),
Charset.forName(charset));) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String now = sdf.format(new Date());
String romdom = romdom();
LOGGER.info("[ServiceImpl][zip]:-------任務id="
+ romdom);
ZipEntry entry = null;
String uuid = null;
while ((entry = zip.getNextEntry()) != null) {
uuid = UUID.randomUUID().toString().replace("-", "");// 流水號
String name = entry.getName();
String zipEntryName = name.replace(
name.substring(0, name.lastIndexOf(".")), uuid);
String outPath = (Constants
.getAppConfigProperty("SAVEPATH") + File.separator + romdom
+ File.separator + zipEntryName);
String savePath = (File.separator + romdom + File.separator + zipEntryName);// 儲存的路徑,用相對路徑
HashMap<String, Object> fileMap = new HashMap<String, Object>();
fileMap.put("KEY", userInfo.getName());// key
fileMap.put("ID", romdom);// 任務id
fileMap.put("CODE", userInfo.getCode());// 賬號
fileMap.put("INSERT_TIME", now);
fileMap.put("ID", uuid);// 主鍵
fileMap.put("FILE_PATH", savePath);// 檔案路徑
fileList.add(fileMap);
// 判斷路徑是否存在,不存在則建立檔案路徑
File file = new File(outPath.substring(0,
outPath.lastIndexOf(File.separator)));
if (!file.exists()) {
file.mkdirs();
}
// 判斷檔案全路徑是否為資料夾,如果是上面已經上傳,不需要解壓
if (new File(outPath).isDirectory()) {
continue;
}
// 輸出檔案路徑資訊
LOGGER.info("[ServiceImpl][uploadVoice]:-------輸出檔案路徑資訊="
+ outPath);
try (OutputStream out = new FileOutputStream(outPath);){
byte[] buf = new byte[1024];
int len = 0;
while ((len = zip.read(buf)) != -1) {
out.write(buf, 0, len);
}
} catch (Exception e) {
LOGGER.error("[ServiceImpl][uploadVoice]:-------上傳zip錯誤=" , e);
return null;
}
}
LOGGER.info("[ServiceImpl][uploadVoice]:-------上傳完畢,檔案的資訊集合="
+ fileList);
} catch (Exception e) {
LOGGER.error("[ServiceImpl][uploadVoice]:-------上傳zip錯誤=" , e);
fileList = null;
}
return fileList;
}
public List<HashMap<String, Object>> rar(MultipartFile file, UserInfo userInfo) {
LOGGER.info("[ServiceImpl][rar]:-------獲取的上傳路徑uploadPath="
+ Constants
.getAppConfigProperty("SAVEPATH"));
List<HashMap<String, Object>> fileList = new ArrayList<HashMap<String, Object>>();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String now = sdf.format(new Date());
String romdom = romdom();
LOGGER.info("[ServiceImpl][uploadVoice]:-------任務id="
+ romdom);
String rarSavePath = Constants.getAppConfigProperty("SAVEPATH") + File.separator + romdom;
File pathFile = new File(rarSavePath + File.separator + file.getOriginalFilename());
if(!pathFile.exists()) {
pathFile.mkdirs();
}
try {
file.transferTo(pathFile);//暫時儲存rar
try (Archive archive = new Archive(pathFile)){
FileHeader fh = archive.nextFileHeader();
File destFileName = null;
String uuid = null;
while (fh != null) {
uuid = UUID.randomUUID().toString().replace("-", "");// 流水號
String name = fh.getFileNameString();
String zipEntryName = name.replace(
name.substring(0, name.lastIndexOf(".")), uuid);
String outPath = rarSavePath + File.separator + zipEntryName;
String savePath = (File.separator + romdom + File.separator + zipEntryName);// 儲存的路徑,用相對路徑
HashMap<String, Object> fileMap = new HashMap<String, Object>();
fileMap.put("KEY", userInfo.getEntName());// key
fileMap.put("TASK_ID", romdom);// 任務id
fileMap.put("CODE", userInfo.getUserCode());//賬號
fileMap.put("INSERT_TIME", now);
fileMap.put("ID", uuid);// 主鍵
fileMap.put("FILE_PATH", savePath);// 檔案路徑
fileList.add(fileMap);
destFileName = new File(outPath);
if (fh.isDirectory()) {
if (!destFileName.exists()) {
destFileName.mkdirs();
}
fh = archive.nextFileHeader();
continue;
}
if (!destFileName.getParentFile().exists()) {
destFileName.getParentFile().mkdirs();
}
try (FileOutputStream fos = new FileOutputStream(destFileName)){
archive.extractFile(fh, fos);
}
fh = archive.nextFileHeader();
}
}
} catch (Exception e) {
LOGGER.error("[ServiceImpl][uploadVoice]:-------上傳rar錯誤=" , e);
fileList = null;
} finally{
pathFile.delete();
}
return fileList;
}