SceneDownloadHandlerServiceImpl.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. package com.fdkankan.download.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.collection.ConcurrentHashSet;
  4. import cn.hutool.core.exceptions.ExceptionUtil;
  5. import cn.hutool.core.util.StrUtil;
  6. import cn.hutool.json.JSONObject;
  7. import cn.hutool.json.JSONUtil;
  8. import com.alibaba.fastjson.JSON;
  9. import com.fdkankan.common.bean.DownLoadProgressBean;
  10. import com.fdkankan.common.bean.DownLoadTaskBean;
  11. import com.fdkankan.common.constant.SceneDownloadProgressStatus;
  12. import com.fdkankan.common.constant.SceneFrom;
  13. import com.fdkankan.common.constant.SceneKind;
  14. import com.fdkankan.common.constant.SceneResolution;
  15. import com.fdkankan.common.constant.ServerCode;
  16. import com.fdkankan.common.constant.UploadFilePath;
  17. import com.fdkankan.common.response.ResultData;
  18. import com.fdkankan.common.util.FileUtils;
  19. import com.fdkankan.download.bean.CurrentDownloadNumUtil;
  20. import com.fdkankan.download.bean.ImageType;
  21. import com.fdkankan.download.bean.ImageTypeDetail;
  22. import com.fdkankan.fyun.constant.StorageType;
  23. import com.fdkankan.fyun.oss.UploadToOssUtil;
  24. import com.fdkankan.redis.constant.RedisKey;
  25. import com.fdkankan.redis.util.RedisUtil;
  26. import com.fdkankan.scene.api.dto.SceneInfoDTO;
  27. import com.fdkankan.scene.api.feign.SceneUserSceneClient;
  28. import com.google.common.collect.Lists;
  29. import java.io.File;
  30. import java.io.FileInputStream;
  31. import java.math.BigDecimal;
  32. import java.net.URLEncoder;
  33. import java.nio.charset.StandardCharsets;
  34. import java.util.ArrayList;
  35. import java.util.Calendar;
  36. import java.util.HashMap;
  37. import java.util.List;
  38. import java.util.Map;
  39. import java.util.Objects;
  40. import java.util.Set;
  41. import java.util.concurrent.Callable;
  42. import java.util.concurrent.ExecutorService;
  43. import java.util.concurrent.Executors;
  44. import java.util.concurrent.Future;
  45. import java.util.concurrent.atomic.AtomicInteger;
  46. import java.util.stream.Collectors;
  47. import lombok.extern.slf4j.Slf4j;
  48. import lombok.var;
  49. import org.apache.tools.zip.ZipOutputStream;
  50. import org.springframework.beans.factory.annotation.Autowired;
  51. import org.springframework.beans.factory.annotation.Value;
  52. import org.springframework.cloud.context.config.annotation.RefreshScope;
  53. import org.springframework.scheduling.annotation.Async;
  54. import org.springframework.stereotype.Service;
  55. import org.springframework.web.client.RestTemplate;
  56. /**
  57. * <p>
  58. * TODO
  59. * </p>
  60. *
  61. * @author dengsixing
  62. * @since 2022/2/22
  63. **/
  64. @RefreshScope
  65. @Slf4j
  66. @Service
  67. public class SceneDownloadHandlerServiceImpl {
  68. private static final String[] prefixArr = new String[]{
  69. UploadFilePath.DATA_VIEW_PATH,
  70. UploadFilePath.VOICE_VIEW_PATH,
  71. UploadFilePath.VIDEOS_VIEW_PATH,
  72. UploadFilePath.IMG_VIEW_PATH,
  73. UploadFilePath.USER_VIEW_PATH,
  74. };
  75. private static final List<ImageType> imageTypes = Lists.newArrayList();
  76. static{
  77. imageTypes.add(ImageType.builder().name("2k_face").size("2048").ranges(new String[]{"0", "511", "1023", "1535"}).build());
  78. imageTypes.add(ImageType.builder().name("1k_face").size("1024").ranges(new String[]{"0", "511"}).build());
  79. imageTypes.add(ImageType.builder().name("512_face").size("512").ranges(new String[]{"0"}).build());
  80. }
  81. @Autowired
  82. private SceneUserSceneClient sceneUserSceneClient;
  83. @Value("${path.v4school}")
  84. private String v4localPath;
  85. @Value("${path.zip-local}")
  86. private String zipLocalFormat;
  87. @Value("${path.zip-oss}")
  88. private String zipOssFormat;
  89. @Value("${path.zip-root}")
  90. private String wwwroot;
  91. @Value("${zip.nThreads}")
  92. private int zipNthreads;
  93. @Value("${oss.bucket:4dkankan}")
  94. private String bucket;
  95. @Value("${upload.type:oss}")
  96. private String uploadType;
  97. @Value("${download.config.resource-url}")
  98. private String resourceUrl;
  99. @Value("${download.config.public-url}")
  100. private String publicUrl;
  101. @Value("${download.config.exe-name}")
  102. private String exeName;
  103. @Value("${download.config.exe-content}")
  104. private String exeContent;
  105. @Autowired
  106. RestTemplate restTemplate;
  107. @Autowired
  108. RedisUtil redisUtil;
  109. @Autowired
  110. UploadToOssUtil uploadToOssUtil;
  111. @Async("sceneDownLoadExecutror")
  112. public void download(DownLoadTaskBean downLoadTaskBean){
  113. //场景码
  114. String num = null;
  115. try {
  116. num = downLoadTaskBean.getNum();
  117. log.info("场景下载开始 - num[{}] - threadName[{}]", num, Thread.currentThread().getName());
  118. long startTime = Calendar.getInstance().getTimeInMillis();
  119. //执行场景下载逻辑
  120. this.downloadHandler(downLoadTaskBean);
  121. //耗时
  122. long consumeTime = Calendar.getInstance().getTimeInMillis() - startTime;
  123. log.info("场景下载结束 - num[{}] - threadName[{}] - consumeTime[{}]", num, Thread.currentThread().getName(), consumeTime);
  124. }catch (Exception e){
  125. log.error(ExceptionUtil.stacktraceToString(e));
  126. }finally {
  127. if(StrUtil.isNotEmpty(num)){
  128. //本地正在下载任务出队
  129. CurrentDownloadNumUtil.removeSceneNum(num);
  130. //删除正在下载任务
  131. redisUtil.lRemove(RedisKey.SCENE_DOWNLOAD_ING, 1, num);
  132. }
  133. }
  134. }
  135. public void downloadHandler(DownLoadTaskBean downLoadTaskBean) throws Exception{
  136. String num = downLoadTaskBean.getNum();
  137. //zip包路径
  138. String zipPath = null;
  139. try {
  140. Set<String> cacheKeys = new ConcurrentHashSet<>();
  141. Map<String, List<String>> allFiles = this.getAllFiles(num, v4localPath);
  142. List<String> ossFilePaths = allFiles.get("ossFilePaths");
  143. List<String> v3localFilePaths = allFiles.get("v3localFilePaths");
  144. //key总个数
  145. int total = ossFilePaths.size() + v3localFilePaths.size();
  146. AtomicInteger count = new AtomicInteger(0);
  147. //定义压缩包
  148. zipPath = String.format(this.zipLocalFormat, num);
  149. File zipFile = new File(zipPath);
  150. if(!zipFile.getParentFile().exists()){
  151. zipFile.getParentFile().mkdirs();
  152. }
  153. ZipOutputStream out = new ZipOutputStream(zipFile);
  154. // JSONObject getInfoJson = this.zipGetInfoJson(out, this.wwwroot, num);
  155. String sceneJsonData = uploadToOssUtil.getObjectContent(bucket, String.format(UploadFilePath.DATA_VIEW_PATH, num) + "scene.json");
  156. JSONObject sceneJson = JSONUtil.parseObj(sceneJsonData);
  157. String resolution = "4k";
  158. String sceneForm = sceneJson.getStr("sceneFrom");
  159. if(StrUtil.isNotEmpty(sceneForm) && SceneFrom.PRO.code().equals(sceneForm)){
  160. resolution = "2k";
  161. }
  162. //如果是已经切好图的场景,不需要再切图了
  163. // String sceneResolution = sceneJson.getStr("sceneResolution");
  164. // if(SceneResolution.TILES.code().equals(sceneResolution)){
  165. // resolution = "notNeadCut";
  166. // }
  167. String sceneKind = sceneJson.getStr("sceneKind");
  168. if(StrUtil.isNotEmpty(sceneKind) && SceneKind.FACE.code().equals(sceneKind)){
  169. resolution = "notNeadCut";
  170. }
  171. int imagesVersion = -1;
  172. // TODO: 2022/3/29 V4版本目前没有imagesVersion字段,暂时用version字段替代
  173. // if(getInfoJson.getInt("imagesVersion") != null){
  174. // imagesVersion = getInfoJson.getInt("imagesVersion");
  175. // }
  176. Integer version = sceneJson.getInt("version");
  177. if(Objects.nonNull(version)){
  178. imagesVersion = version;
  179. }
  180. long start = Calendar.getInstance().getTimeInMillis();
  181. //固定文件写入
  182. this.zipLocalFiles(out, v3localFilePaths, v4localPath, num, count, total);
  183. long end1 = Calendar.getInstance().getTimeInMillis();
  184. log.info("打包固定文件耗时, num:{}, time:{}", num, end1 - start);
  185. //oss文件写入
  186. this.zipOssFiles(out, ossFilePaths, num, count, total, resolution, imagesVersion, cacheKeys);
  187. long end2 = Calendar.getInstance().getTimeInMillis();
  188. log.info("打包oss文件耗时, num:{}, time:{}", num, end2 - end1);
  189. //重新写入scene.json(去掉密码访问设置)
  190. this.zipSceneJson(out, this.wwwroot, num, sceneJson);
  191. //写入启动命令
  192. this.zipBat(out, num);
  193. out.close();
  194. //上传压缩包
  195. String uploadPath = String.format(this.zipOssFormat, num);
  196. uploadToOssUtil.uploadBySh(zipPath, uploadPath);
  197. //更新进度100
  198. String url = this.publicUrl + uploadPath + "?t=" + Calendar.getInstance().getTimeInMillis();
  199. this.updateProgress(null, num, SceneDownloadProgressStatus.DOWNLOAD_SUCCESS.code(), url);
  200. // TODO: 2022/5/24 v3 停止后要开启-----------------------start
  201. //更新用户场景已下载次数
  202. // platformUserClient.updateDownloadNum(userId, 1);
  203. //
  204. // //更新下载log状态为成功
  205. // sceneUserSceneClient.updateSceneDownloadLog(num, DownloadStatus.SUCCESS.code(), url, null);
  206. // TODO: 2022/5/24 v3 停止后要开启-----------------------end
  207. }catch (Exception e){
  208. log.error("下载失败,num={}", num);
  209. //更新进度为下载失败
  210. this.updateProgress( null, num, SceneDownloadProgressStatus.DOWNLOAD_FAILED.code(), null);
  211. //更新下载log状态为成功
  212. // TODO: 2022/5/24 v3 停止后要开启-----------------------start
  213. // sceneUserSceneClient.updateSceneDownloadLog(num, DownloadStatus.FAILD.code(), null, ExceptionUtil.stacktraceToString(e));
  214. // TODO: 2022/5/24 v3 停止后要开启-----------------------send
  215. throw e;
  216. }finally {
  217. if(StrUtil.isNotBlank(zipPath)){
  218. //删除本地zip包
  219. FileUtils.deleteFile(zipPath);
  220. }
  221. }
  222. }
  223. private void zipOssFiles(ZipOutputStream out, List<String> ossFilePaths, String num, AtomicInteger count,
  224. int total, String resolution, int imagesVersion, Set<String> cacheKeys) throws Exception{
  225. if(CollUtil.isEmpty(ossFilePaths)){
  226. return;
  227. }
  228. String imageNumPath = String.format(UploadFilePath.IMG_VIEW_PATH, num);
  229. ExecutorService executorService = Executors.newFixedThreadPool(this.zipNthreads);
  230. List<Future> futureList = new ArrayList<>();
  231. for (String filePath : ossFilePaths) {
  232. Callable<Boolean> call = new Callable() {
  233. @Override
  234. public Boolean call() throws Exception {
  235. zipOssFilesHandler(out, num, count, total, resolution,
  236. imagesVersion, cacheKeys,filePath, imageNumPath);
  237. return true;
  238. }
  239. };
  240. futureList.add(executorService.submit(call));
  241. }
  242. //这里一定要加阻塞,不然会导致oss文件还没打包好,主程序已经结束返回了
  243. Boolean zipSuccess = true;
  244. for (Future future : futureList) {
  245. try {
  246. future.get();
  247. }catch (Exception e){
  248. log.error("打包oss文件失败", e);
  249. zipSuccess = false;
  250. }
  251. }
  252. if(!zipSuccess){
  253. throw new Exception("打包oss文件失败");
  254. }
  255. }
  256. private void zipOssFilesHandler(ZipOutputStream out, String num,
  257. AtomicInteger count, int total, String resolution,
  258. int imagesVersion, Set<String> cacheKeys,
  259. String filePath, String imageNumPath) throws Exception{
  260. //更新进度
  261. this.updateProgress(new BigDecimal(count.incrementAndGet()).divide(new BigDecimal(total), 6, BigDecimal.ROUND_HALF_UP),
  262. num, SceneDownloadProgressStatus.DOWNLOADING.code(), null);
  263. //某个目录不需要打包
  264. if(filePath.contains(imageNumPath + "panorama/panorama_edit/"))
  265. return;
  266. //切图
  267. if(!"notNeadCut".equals(resolution)){
  268. if((filePath.contains(imageNumPath + "panorama/") && filePath.contains("tiles/" + resolution))
  269. || filePath.contains(imageNumPath + "tiles/" + resolution + "/")) {
  270. this.processImage(filePath, out, resolution, imagesVersion, cacheKeys);
  271. return;
  272. }
  273. }
  274. //其他文件打包
  275. this.ProcessFiles(num, filePath, out, this.wwwroot, cacheKeys);
  276. }
  277. private void zipLocalFiles(ZipOutputStream out, List<String> v3localFilePaths, String v3localPath, String num, AtomicInteger count, int total) throws Exception{
  278. for (String v3localFilePath : v3localFilePaths) {
  279. try (FileInputStream in = new FileInputStream(new File(v3localFilePath));){
  280. this.zipInputStream(out, v3localFilePath.replace(v3localPath, ""), in);
  281. }catch (Exception e){
  282. throw e;
  283. }
  284. //更新进度
  285. this.updateProgress(
  286. new BigDecimal(count.incrementAndGet()).divide(new BigDecimal(total), 6, BigDecimal.ROUND_HALF_UP),
  287. num, SceneDownloadProgressStatus.DOWNLOAD_COMPRESSING.code(), null);
  288. }
  289. //写入code.txt
  290. this.zipBytes(out, "code.txt", num.getBytes());
  291. }
  292. private void zipBat(ZipOutputStream out, String num) throws Exception{
  293. String batContent = String.format(this.exeContent, num);
  294. this.zipBytes(out, exeName, batContent.getBytes());
  295. //更新进度为90%
  296. this.updateProgress(new BigDecimal("0.9").divide(new BigDecimal("0.8"), 6, BigDecimal.ROUND_HALF_UP), num,
  297. SceneDownloadProgressStatus.DOWNLOAD_COMPRESSING.code(), null);
  298. }
  299. private Map<String, List<String>> getAllFiles(String num, String v3localPath) throws Exception{
  300. //列出oss所有文件路径
  301. List<String> ossFilePaths = new ArrayList<>();
  302. for (String prefix : prefixArr) {
  303. prefix = String.format(prefix, num);
  304. List<String> keys = uploadToOssUtil.listKeys(prefix);
  305. if(CollUtil.isEmpty(keys)){
  306. continue;
  307. }
  308. if(StorageType.AWS.code().equals(this.uploadType)){
  309. keys = keys.stream().filter(key->{
  310. if(key.contains("x-oss-process")){
  311. return false;
  312. }
  313. return true;
  314. }).collect(Collectors.toList());
  315. }
  316. ossFilePaths.addAll(keys);
  317. }
  318. //列出v3local所有文件路径
  319. File file = new File(v3localPath);
  320. List<String> v3localFilePaths = FileUtils.list(file);
  321. HashMap<String, List<String>> map = new HashMap<>();
  322. map.put("ossFilePaths", ossFilePaths);
  323. map.put("v3localFilePaths", v3localFilePaths);
  324. return map;
  325. }
  326. private JSONObject zipGetInfoJson(ZipOutputStream out, String root, String num) throws Exception{
  327. ResultData<SceneInfoDTO> sceneViewInfo = sceneUserSceneClient.getSceneViewInfo(num);
  328. if(!sceneViewInfo.getSuccess()){
  329. throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
  330. }
  331. SceneInfoDTO data = sceneViewInfo.getData();
  332. JSONObject getInfoJson = null;
  333. if(Objects.isNull(data)){
  334. getInfoJson = new JSONObject();
  335. }else {
  336. getInfoJson = JSONUtil.parseObj(data);
  337. }
  338. getInfoJson.set("sceneScheme", 3);
  339. getInfoJson.set("needKey", 0);
  340. getInfoJson.set("sceneKey","");
  341. //写入getInfo.json
  342. String getInfoJsonPath = root + String.format(UploadFilePath.DATA_VIEW_PATH, num) + "getInfo.json";
  343. this.zipBytes(out, getInfoJsonPath, getInfoJson.toString().getBytes());
  344. return getInfoJson;
  345. }
  346. private void zipSceneJson(ZipOutputStream out, String root, String num, JSONObject sceneJson) throws Exception{
  347. //访问密码置0
  348. JSONObject controls = sceneJson.getJSONObject("controls");
  349. controls.set("showLock", 0);
  350. String sceneJsonPath = root + String.format(UploadFilePath.DATA_VIEW_PATH, num) + "scene.json";
  351. this.zipBytes(out, sceneJsonPath, sceneJson.toString().getBytes());
  352. }
  353. private void processImage(String key, ZipOutputStream out, String resolution, int imagesVersion, Set<String> imgKeys) throws Exception{
  354. if(key.contains("x-oss-process") || key.endsWith("/")){
  355. return;
  356. }
  357. String fileName = key.substring(key.lastIndexOf("/")+1, key.indexOf("."));
  358. String ext = key.substring(key.lastIndexOf("."));
  359. String[] arr = fileName.split("_skybox");
  360. String dir = arr[0];
  361. String num = arr[1];
  362. if(StrUtil.isEmpty(fileName)
  363. || StrUtil.isEmpty(ext)
  364. || (".jpg".equals(ext) && ".png".equals(ext))
  365. || StrUtil.isEmpty(dir)
  366. || StrUtil.isEmpty(num)){
  367. throw new Exception("本地下载图片资源不符合规则,key:" + key);
  368. }
  369. for (ImageType imageType : imageTypes) {
  370. List<ImageTypeDetail> items = Lists.newArrayList();
  371. String[] ranges = imageType.getRanges();
  372. for(int i = 0; i < ranges.length; i++){
  373. String x = ranges[i];
  374. for(int j = 0; j < ranges.length; j++){
  375. String y = ranges[j];
  376. items.add(
  377. ImageTypeDetail.builder()
  378. .i(String.valueOf(i))
  379. .j(String.valueOf(j))
  380. .x(x)
  381. .y(y)
  382. .build()
  383. );
  384. }
  385. }
  386. for (ImageTypeDetail item : items) {
  387. String par = "?x-oss-process=image/resize,m_lfit,w_" + imageType.getSize() + "/crop,w_512,h_512,x_" + item.getX() + ",y_" + item.getY();
  388. if(StorageType.AWS.code().equals(uploadType)){
  389. par += "&imagesVersion="+ imagesVersion;
  390. }
  391. var url = this.
  392. resourceUrl + key;
  393. StorageType storageType = StorageType.get(uploadType);
  394. switch (storageType){
  395. case OSS:
  396. url += par;
  397. break;
  398. case AWS:
  399. url += URLEncoder.encode(par.replace("/", "@"), "UTF-8");
  400. break;
  401. }
  402. var fky = key.split("/" + resolution + "/")[0] + "/" + dir + "/" + imageType.getName() + num + "_" + item.getI() + "_" + item.getJ() + ext;
  403. if(imgKeys.contains(fky)){
  404. continue;
  405. }
  406. imgKeys.add(fky);
  407. this.zipBytes(out, wwwroot + fky, FileUtils.getBytesFromUrl(url));
  408. }
  409. }
  410. }
  411. public void ProcessFiles(String num, String key, ZipOutputStream out, String prefix, Set<String> cacheKeys) throws Exception{
  412. if(cacheKeys.contains(key)){
  413. return;
  414. }
  415. if(key.equals(String.format(UploadFilePath.DATA_VIEW_PATH, num) + "scene.json")){
  416. return;
  417. }
  418. cacheKeys.add(key);
  419. String fileName = key.substring(key.lastIndexOf("/") + 1);
  420. String url = this.resourceUrl + key.replace(fileName, URLEncoder.encode(fileName, "UTF-8")) + "?t=" + Calendar.getInstance().getTimeInMillis();
  421. if(key.contains("hot.json") || key.contains("link-scene.json")){
  422. String content = FileUtils.getStringFromUrl(url);
  423. content.replace(publicUrl, "")
  424. // .replace(publicUrl+"v3/", "")
  425. .replace("https://spc.html","spc.html")
  426. .replace("https://smobile.html", "smobile.html");
  427. zipBytes(out, prefix + key, content.getBytes());
  428. }else{
  429. zipBytes(out, prefix + key, FileUtils.getBytesFromUrl(url));
  430. }
  431. }
  432. public void updateProgress(BigDecimal precent, String num, Integer status, String url){
  433. SceneDownloadProgressStatus progressStatus = SceneDownloadProgressStatus.get(status);
  434. switch (progressStatus){
  435. case DOWNLOAD_SUCCESS:
  436. precent = new BigDecimal("100");
  437. break;
  438. case DOWNLOAD_FAILED:
  439. log.info("失败了啊-------------------------------");
  440. precent = new BigDecimal("0");
  441. break;
  442. default:
  443. precent = precent.multiply(new BigDecimal("0.8")).multiply(new BigDecimal("100"));
  444. }
  445. DownLoadProgressBean progress = null;
  446. String key = String.format(RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4, num);
  447. String progressStr = redisUtil.get(key);
  448. if(StrUtil.isEmpty(progressStr)){
  449. progress = DownLoadProgressBean.builder().percent(precent.intValue()).status(status).url(url).build();
  450. }else{
  451. progress = JSONUtil.toBean(progressStr, DownLoadProgressBean.class);
  452. //如果下载失败,进度不变
  453. if(status == SceneDownloadProgressStatus.DOWNLOAD_FAILED.code() && progress.getPercent() != null){
  454. precent = new BigDecimal(progress.getPercent());
  455. }
  456. progress.setPercent(precent.intValue());
  457. progress.setStatus(status);
  458. progress.setUrl(url);
  459. }
  460. if(status == 1003){
  461. log.info(JSONUtil.toJsonStr(progress));
  462. }
  463. redisUtil.set(key, JSONUtil.toJsonStr(progress));
  464. }
  465. public void zipInputStream(ZipOutputStream out, String key, FileInputStream in) throws Exception {
  466. out.putNextEntry(new org.apache.tools.zip.ZipEntry(key));
  467. byte[] bytes = new byte[1024];
  468. int b = 0;
  469. while ((b = in.read(bytes)) != -1) {
  470. out.write(bytes, 0, b);
  471. }
  472. }
  473. public synchronized void zipBytes(ZipOutputStream out, String key, byte[] bytes) throws Exception {
  474. if(Objects.isNull(bytes)){
  475. log.error("获取oss字节数组为空,key={}",key);
  476. }
  477. out.putNextEntry(new org.apache.tools.zip.ZipEntry(key));
  478. out.write(bytes);
  479. }
  480. }