/**
* 创建zip附件文件夹
* @param projectId 项目id
* @param packageId 分包标段id
* @return
*/
public String createZipFolder(Integer projectId,Integer packageId){
try {
if(null!=projectId){
//项目信息
Project project = projectMapper.selectByPrimaryKey(projectId);
if(null!=project) {
//项目路径
String projectName = project.getName();
String projectPath = filePath + File.separator + projectName;
List<ProjectSubpackage> packages = new ArrayList<>();
//查询项目分包列表
if(null!=packageId){
ProjectSubpackage subpackage = projectSubpackageMapper.selectByPrimaryKey(packageId);
packages.add(subpackage);
}else {
packages = projectSubpackageMapper.selectProjectSubByProgectId(projectId);
}
if (null!=packages) {
for (ProjectSubpackage subpackage : packages) {
String packageName = subpackage.getDeviceName();
Integer subpackageId = subpackage.getId();
//分包路径
String packagePath = projectPath + File.separator + packageName;
//查询分包标段下的供应商信息
List<Integer> supplierIdList = projectSupplierMapper.selectSupplierAccountByProjectId(subpackageId);
if(null!=supplierIdList){
for (Integer supplierId : supplierIdList) {
Supplier supplier = supplierMapper.selectByPrimaryKey(supplierId);
String supplierName = supplier.getUnitName();
//供应商路径
String folderpath = packagePath + File.separator + supplierName;
//商务
String businessFolderpath = folderpath + File.separator + "商务";
//技术
String technologyFolderpath = folderpath + File.separator + "技术";
File businessFile = new File(businessFolderpath);
if (!businessFile.exists()) {
businessFile.mkdirs();
}
File technologyFile = new File(technologyFolderpath);
if (!technologyFile.exists()) {
technologyFile.mkdirs();
}
BusinessTechnologyAtta businessTechnologyAtta = new BusinessTechnologyAtta();
businessTechnologyAtta.setSupplierId(supplierId.longValue());
businessTechnologyAtta.setStatus(0);
//查询附件信息
List<BusinessTechnologyAtta> attalist = getBusinessTechnologyAttalist(businessTechnologyAtta);
if(null!=attalist && attalist.size()>0) {
for (BusinessTechnologyAtta atta : attalist) {
String attaPath = atta.getAttachmentPath();
String attaName = atta.getAttachmentName();
Integer attaType = atta.getAttachmentType();
//拷贝原文件到待压缩文件夹
if (new File(attaPath).exists()) {
String newfolderpath = null;
if (1 == attaType) {
newfolderpath = businessFolderpath + File.separator + attaName;
}
if (2 == attaType) {
newfolderpath = technologyFolderpath + File.separator + attaName;
}
FileCopyUtils.copy(new File(attaPath), new File(newfolderpath));
//Files.copy(Paths.get(attaPath), Paths.get(newfolderpath));
}
}
}
}
}
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
String zipPath = projectPath + sdf.format(new Date()) + ".zip";
boolean zipAll = zipAll(zipPath, projectPath);
if(zipAll){
File file = new File(projectPath);
FileUtils.deleteDirectory(file);
//FileUtils.forceDelete(file);
return zipPath;
}
}
}
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
/**
* 创建Zip文件
*/
private static boolean zipAll(String folderName, String folder) {
File folderParam = new File(folder);
if(folderParam.isDirectory()){
try{
File[] files = folderParam.listFiles();
// 生成的压缩文件
ZipFile zipFile = new ZipFile(folderName);
ZipParameters parameters = new ZipParameters();
// 压缩方式
parameters.setCompressionMethod(CompressionMethod.DEFLATE);
// 压缩级别
parameters.setCompressionLevel(CompressionLevel.NORMAL);
// 遍历文件夹下所有的文件、文件夹
for(File file: files){
if (file.isDirectory()) {
zipFile.addFolder(file, parameters);
} else {
zipFile.addFile(file, parameters);
}
}
return true;
}catch (ZipException e){
e.printStackTrace();
}
}
return false;
}
创建ZIP压缩文件及文件夹
于 2023-11-10 16:06:18 首次发布