From 105fc752691e15d79715827dc58f22c9a2b87b96 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期一, 03 四月 2023 14:20:29 +0800 Subject: [PATCH] 1 --- src/main/java/com/lf/server/service/show/DataLibService.java | 31 --------------- src/main/java/com/lf/server/service/data/DownloadService.java | 79 ++++++++++++++++++++++++++------------- 2 files changed, 53 insertions(+), 57 deletions(-) diff --git a/src/main/java/com/lf/server/service/data/DownloadService.java b/src/main/java/com/lf/server/service/data/DownloadService.java index 8383fa7..9a0a4b1 100644 --- a/src/main/java/com/lf/server/service/data/DownloadService.java +++ b/src/main/java/com/lf/server/service/data/DownloadService.java @@ -10,7 +10,6 @@ import com.lf.server.entity.sys.UserEntity; import com.lf.server.helper.*; import com.lf.server.mapper.data.DownloadMapper; -import com.lf.server.service.show.DataLibService; import com.lf.server.service.sys.MetaDownService; import net.lingala.zip4j.ZipFile; import net.lingala.zip4j.model.FileHeader; @@ -21,6 +20,7 @@ import org.springframework.stereotype.Service; import java.io.File; +import java.io.FileInputStream; import java.util.*; /** @@ -34,9 +34,6 @@ @Autowired DownloadMapper downloadMapper; - - @Autowired - DataLibService dataLibService; @Autowired MetaDownService metaDownService; @@ -201,10 +198,6 @@ Map<String, List<AttachEntity>> annexMap = new HashMap<>(2); queryData(tabs, dataMap, annexMap); - //String downloadPath = pathHelper.getDownloadFullPath(); - //String zipName = StringHelper.YMDHMS2_FORMAT.format(new Date()) + ".zip"; - //String zipFile = downloadPath + File.separator + zipName; - String tempName = StringHelper.YMDHMS2_FORMAT.format(new Date()); String tempPath = pathHelper.getTempPath(tempName); String gdbPath = tempPath + File.separator + "tabs.gdb"; @@ -217,12 +210,12 @@ GdbHelper.createGdb(gdbPath, dataMap); } - String zipFile = pathHelper.getDownloadFullPath() + File.separator + tempName + ".gdb.zip"; + String zipFile = pathHelper.getDownloadFullPath() + File.separator + tempName + ".zip"; ZipFile zip = Zip4jHelper.createZipFile(zipFile, pwd); ZipParameters params = Zip4jHelper.getZipParams(true); - //addMetaFiles(zip, params, list); + addMetaFiles(zip, params, list); zip.addFolder(new File(gdbPath), params); - dataLibService.addAnnex(zip, params, annexMap); + addAnnex(zip, params, annexMap); String dbPwd = Md5Helper.reverse(Md5Helper.generate(pwd)); DownloadEntity de = getDownloadEntity(ue, zipFile, dbPwd); @@ -284,37 +277,69 @@ } /** + * 娣诲姞闄勪欢 + */ + public void addAnnex(ZipFile zip, ZipParameters params, Map<String, List<AttachEntity>> annexMap) { + List<String> files = new ArrayList<>(); + String uploadPath = pathHelper.getConfig().getUploadPath(); + for (String key : annexMap.keySet()) { + for (AttachEntity ae : annexMap.get(key)) { + try { + File srcFile = new File(uploadPath + File.separator + ae.getPath()); + if (!srcFile.exists() || srcFile.isDirectory()) { + continue; + } + if (files.contains(srcFile.getPath())) { + continue; + } + + files.add(srcFile.getPath()); + params.setFileNameInZip("annex" + File.separator + key + File.separator + ae.getName()); + zip.addStream(new FileInputStream(srcFile), params); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + } + } + } + + /** * 娣诲姞鍏冩暟鎹枃浠惰嚦Zip鍖� */ private void addMetaFiles(ZipFile zip, ZipParameters params, List<MetaEntity> list) { - int i = 1; + List<String> names = new ArrayList<>(); String uploadPath = pathHelper.getConfig().getUploadPath(); for (MetaEntity mf : list) { + if (names.contains(mf.getName())) { + mf.setName(mf.getId() + "_" + mf.getName()); + } else { + names.add(mf.getName()); + } + try { switch ("." + mf.getType()) { case StaticData.MPT: - addMultiFile(i++, uploadPath, mf, zip, params, StaticData.MPT_EXT); + addMultiFile(uploadPath, mf, zip, params, StaticData.MPT_EXT); break; case StaticData.IMG: - addMultiFile(i++, uploadPath, mf, zip, params, StaticData.IMG_EXT); + addMultiFile(uploadPath, mf, zip, params, StaticData.IMG_EXT); break; case StaticData.TIF: - addMultiFile(i++, uploadPath, mf, zip, params, StaticData.TIF_EXT); + addMultiFile(uploadPath, mf, zip, params, StaticData.TIF_EXT); break; case StaticData.TIFF: - addMultiFile(i++, uploadPath, mf, zip, params, StaticData.TIFF_EXT); + addMultiFile(uploadPath, mf, zip, params, StaticData.TIFF_EXT); break; case StaticData.SHP: - addMultiFile(i++, uploadPath, mf, zip, params, StaticData.SHP_EXT); + addMultiFile(uploadPath, mf, zip, params, StaticData.SHP_EXT); break; case StaticData.GDB: - addFolderFile(i++, uploadPath, mf, zip, params); + addFolderFile(uploadPath, mf, zip, params); break; default: - addSingleFile(i++, uploadPath, mf, zip, params); + addSingleFile(uploadPath, mf, zip, params); break; } - } catch (Exception ex) { log.error(ex.getMessage(), ex); } @@ -324,7 +349,7 @@ /** * 娣诲姞鐩綍鏂囦欢 */ - private void addFolderFile(int i, String uploadPath, MetaEntity mf, ZipFile zip, ZipParameters params) throws Exception { + private void addFolderFile(String uploadPath, MetaEntity mf, ZipFile zip, ZipParameters params) throws Exception { File file = new File(uploadPath + File.separator + mf.getPath()); if (!file.exists() || !file.isDirectory()) { return; @@ -334,15 +359,15 @@ String fileName = FileHelper.getFileName(file.getPath()); FileHeader header = zip.getFileHeader(fileName); if (null != header) { - zip.renameFile(header, i + "_" + mf.getName()); + zip.renameFile(header, mf.getName()); } } /** * 娣诲姞澶氭枃浠� */ - private void addMultiFile(int i, String uploadPath, MetaEntity mf, ZipFile zip, ZipParameters params, List<String> extList) throws Exception { - addSingleFile(i, uploadPath, mf, zip, params); + private void addMultiFile(String uploadPath, MetaEntity mf, ZipFile zip, ZipParameters params, List<String> extList) throws Exception { + addSingleFile(uploadPath, mf, zip, params); for (String ext : extList) { File file = new File(uploadPath + File.separator + mf.getPath().replace("." + mf.getType(), ext)); @@ -354,7 +379,7 @@ String fileName = FileHelper.getFileName(file.getPath()); FileHeader header = zip.getFileHeader(fileName); if (null != header) { - zip.renameFile(header, i + "_" + mf.getName().replace("." + mf.getType(), ext)); + zip.renameFile(header, mf.getName().replace("." + mf.getType(), ext)); } } } @@ -362,14 +387,14 @@ /** * 娣诲姞鍗曟枃浠� */ - private void addSingleFile(int i, String uploadPath, MetaEntity mf, ZipFile zip, ZipParameters params) throws Exception { + private void addSingleFile(String uploadPath, MetaEntity mf, ZipFile zip, ZipParameters params) throws Exception { File file = new File(uploadPath + File.separator + mf.getPath()); zip.addFile(file, params); String fileName = FileHelper.getFileName(file.getPath()); FileHeader header = zip.getFileHeader(fileName); if (null != header) { - zip.renameFile(header, i + "_" + mf.getName()); + zip.renameFile(header, mf.getName()); } } diff --git a/src/main/java/com/lf/server/service/show/DataLibService.java b/src/main/java/com/lf/server/service/show/DataLibService.java index 11a542f..f6f1fe3 100644 --- a/src/main/java/com/lf/server/service/show/DataLibService.java +++ b/src/main/java/com/lf/server/service/show/DataLibService.java @@ -1,7 +1,6 @@ package com.lf.server.service.show; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; -import com.lf.server.entity.all.BaseEntity; import com.lf.server.entity.ctrl.DownloadReqEntity; import com.lf.server.entity.data.DownloadEntity; import com.lf.server.entity.sys.AttachEntity; @@ -20,7 +19,6 @@ import org.springframework.stereotype.Service; import java.io.File; -import java.io.FileInputStream; import java.util.*; /** @@ -165,7 +163,7 @@ ZipParameters params = Zip4jHelper.getZipParams(true); zip.addFolder(new File(gdbPath), params); // zip.addFolder(new File(annexPath), params) - addAnnex(zip, params, annexMap); + downloadService.addAnnex(zip, params, annexMap); String dbPwd = Md5Helper.reverse(Md5Helper.generate(dr.getPwd())); DownloadEntity de = getDownloadEntity(ue, zipFile, dbPwd); @@ -279,33 +277,6 @@ } FileHelper.copyFile(srcFile, destFile); - } catch (Exception ex) { - log.error(ex.getMessage(), ex); - } - } - } - } - - /** - * 娣诲姞闄勪欢 - */ - public void addAnnex(ZipFile zip, ZipParameters params, Map<String, List<AttachEntity>> annexMap) { - List<String> files = new ArrayList<>(); - String uploadPath = pathHelper.getConfig().getUploadPath(); - for (String key : annexMap.keySet()) { - for (AttachEntity ae : annexMap.get(key)) { - try { - File srcFile = new File(uploadPath + File.separator + ae.getPath()); - if (!srcFile.exists() || srcFile.isDirectory()) { - continue; - } - if (files.contains(srcFile.getPath())) { - continue; - } - - files.add(srcFile.getPath()); - params.setFileNameInZip("annex" + File.separator + key + File.separator + ae.getName()); - zip.addStream(new FileInputStream(srcFile), params); } catch (Exception ex) { log.error(ex.getMessage(), ex); } -- Gitblit v1.9.3