public void receiveFileSynced() {
//插入同步数据
//insertToArchive();
Integer notSyncedCount = sysArchiveService.getNotSyncedCount(BusinessEnum.RECEIVEFILE_DATA_MODEL);
if (notSyncedCount > 0) {
Stream<SysArchive> sysArchiveStream = sysArchiveService.getListByNotSynced(BusinessEnum.RECEIVEFILE_DATA_MODEL).stream();
// 执行任务流
sysArchiveStream.forEach(stream -> {
ReceiveFile receiveFile = receiveFileService.getOne(new LambdaQueryWrapper<ReceiveFile>().eq(ReceiveFile::getRecordId, stream.getRecordId()));
try {
//创建归档数据包目录
String archiveDataPath = FileHelper.makeDir(commonMethod.getTemporaryFilePath(receiveFile.getRecordId()), null);
//元数据xml
String metadataXml = assemblyXmlMetaData(receiveFile, archiveDataPath);
//xml拼装数据正常才同步,否则只插入数据
if (!StringUtils.isEmpty(metadataXml)) {
//保存元数据xml至归档数据包目录
FileDownLoad.fileSave(archiveDataPath, "/" + BusinessEnum.METADATA_FILE_NAME + BusinessEnum.FILE_SUFFIX_XML, metadataXml.getBytes(StandardCharsets.UTF_8));
//压缩文件
String archiveZipPath = archiveDataPath + BusinessEnum.FILE_SUFFIX_ZIP;
String zipName = ZipUtils.zip(new java.io.File(archiveDataPath).listFiles(), new java.io.File(archiveZipPath));
//压缩包上传
Map<String, String> resMap = uploadArchiveData(receiveFile.getRecordId(), zipName, archiveZipPath);
if (resMap != null) {
//归档信息提交
submitArchiveInfo(receiveFile, systemConfig.getDeptCode(), systemConfig.getDeptName(), resMap.get("dataPath")
, resMap.get("checksumType"), resMap.get("checksum"));
}
} else {
//失败则删除已经创建好的临时文件夹
log.warning("收文归档包组装数据失败,重要文件不存在(电子收文件或公文处理单),错误单号:" + receiveFile.getRecordId());
FileHelper.deleteDir(new java.io.File(archiveDataPath));
}
} catch (Exception e) {
log.warning(e.getMessage());
e.printStackTrace();
}
});
}
}
/**
* 打包文件
* @param files 文件或文件夹的集合
* @param out 输出的zip文件
* return 返回压缩后的文件名称
*/
public static String zip(File[] files, File out) {
if (files != null) {
Map<String, File> map = new HashMap<String, File>();
for (File f : files) {
list(f, null, map);
}
if (!map.isEmpty()) {
try {
ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(out);
//解决linux乱码
zaos.setEncoding("GBK");
for (Map.Entry<String, File> entry : map.entrySet()) {
File file = entry.getValue();
ZipArchiveEntry zae = new ZipArchiveEntry(file, entry.getKey());
//解决linux乱码
zae.setUnixMode(644);
zaos.putArchiveEntry(zae);
InputStream is = new FileInputStream(file);
byte[] b = new byte[1024 * 5];
int i = -1;
while ((i = is.read(b)) != -1) {
zaos.write(b, 0, i);
}
is.close();
zaos.closeArchiveEntry();
}
zaos.finish();
zaos.close();
} catch (IOException ex) {
log.warning(ex.getMessage());
}
}
}
return out.getName();
}
/**
* 递归删除目录下的所有文件及子目录下所有文件
* @param dir 将要删除的文件目录
* @return boolean Returns "true" if all deletions were successful.
* If a deletion fails, the method stops attempting to
* delete and returns "false".
*/
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// 目录此时为空,可以删除
return dir.delete();
}
java 文件夹压缩zip格式
于 2022-06-23 16:14:29 首次发布