| | |
| | | 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; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.File; |
| | | import java.io.FileInputStream; |
| | | import java.util.*; |
| | | |
| | | /** |
| | |
| | | |
| | | @Autowired |
| | | DownloadMapper downloadMapper; |
| | | |
| | | @Autowired |
| | | DataLibService dataLibService; |
| | | |
| | | @Autowired |
| | | MetaDownService metaDownService; |
| | |
| | | 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"; |
| | |
| | | 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); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 添加附件 |
| | | */ |
| | | 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); |
| | | } |
| | |
| | | /** |
| | | * 添加目录文件 |
| | | */ |
| | | 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; |
| | |
| | | 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)); |
| | |
| | | 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)); |
| | | } |
| | | } |
| | | } |
| | |
| | | /** |
| | | * 添加单文件 |
| | | */ |
| | | 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()); |
| | | } |
| | | } |
| | | |