SceneDownloadHandlerServiceImpl.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. //更新进度为下载失败
  209. this.updateProgress( null, num, SceneDownloadProgressStatus.DOWNLOAD_FAILED.code(), null);
  210. //更新下载log状态为成功
  211. // TODO: 2022/5/24 v3 停止后要开启-----------------------start
  212. // sceneUserSceneClient.updateSceneDownloadLog(num, DownloadStatus.FAILD.code(), null, ExceptionUtil.stacktraceToString(e));
  213. // TODO: 2022/5/24 v3 停止后要开启-----------------------send
  214. throw e;
  215. }finally {
  216. if(StrUtil.isNotBlank(zipPath)){
  217. //删除本地zip包
  218. FileUtils.deleteFile(zipPath);
  219. }
  220. }
  221. }
  222. private void zipOssFiles(ZipOutputStream out, List<String> ossFilePaths, String num, AtomicInteger count,
  223. int total, String resolution, int imagesVersion, Set<String> cacheKeys) throws Exception{
  224. if(CollUtil.isEmpty(ossFilePaths)){
  225. return;
  226. }
  227. String imageNumPath = String.format(UploadFilePath.IMG_VIEW_PATH, num);
  228. ExecutorService executorService = Executors.newFixedThreadPool(this.zipNthreads);
  229. List<Future> futureList = new ArrayList<>();
  230. for (String filePath : ossFilePaths) {
  231. Callable<Boolean> call = new Callable() {
  232. @Override
  233. public Boolean call() throws Exception {
  234. try {
  235. zipOssFilesHandler(out, num, count, total, resolution,
  236. imagesVersion, cacheKeys,filePath, imageNumPath);
  237. }catch (Exception e){
  238. log.error("下载失败", e);
  239. return false;
  240. }
  241. return true;
  242. }
  243. };
  244. futureList.add(executorService.submit(call));
  245. }
  246. //这里一定要加阻塞,不然会导致oss文件还没打包好,主程序已经结束返回了
  247. Boolean zipSuccess = true;
  248. for (Future future : futureList) {
  249. try {
  250. future.get();
  251. }catch (Exception e){
  252. log.error("打包oss文件失败", e);
  253. zipSuccess = false;
  254. }
  255. }
  256. if(!zipSuccess){
  257. throw new Exception("打包oss文件失败");
  258. }
  259. }
  260. private void zipOssFilesHandler(ZipOutputStream out, String num,
  261. AtomicInteger count, int total, String resolution,
  262. int imagesVersion, Set<String> cacheKeys,
  263. String filePath, String imageNumPath) throws Exception{
  264. //更新进度
  265. this.updateProgress(new BigDecimal(count.incrementAndGet()).divide(new BigDecimal(total), 6, BigDecimal.ROUND_HALF_UP),
  266. num, SceneDownloadProgressStatus.DOWNLOADING.code(), null);
  267. //某个目录不需要打包
  268. if(filePath.contains(imageNumPath + "panorama/panorama_edit/"))
  269. return;
  270. //切图
  271. if(!"notNeadCut".equals(resolution)){
  272. if((filePath.contains(imageNumPath + "panorama/") && filePath.contains("tiles/" + resolution))
  273. || filePath.contains(imageNumPath + "tiles/" + resolution + "/")) {
  274. this.processImage(filePath, out, resolution, imagesVersion, cacheKeys);
  275. return;
  276. }
  277. }
  278. //其他文件打包
  279. this.ProcessFiles(num, filePath, out, this.wwwroot, cacheKeys);
  280. }
  281. private void zipLocalFiles(ZipOutputStream out, List<String> v3localFilePaths, String v3localPath, String num, AtomicInteger count, int total) throws Exception{
  282. for (String v3localFilePath : v3localFilePaths) {
  283. try (FileInputStream in = new FileInputStream(new File(v3localFilePath));){
  284. this.zipInputStream(out, v3localFilePath.replace(v3localPath, ""), in);
  285. }catch (Exception e){
  286. throw e;
  287. }
  288. //更新进度
  289. this.updateProgress(
  290. new BigDecimal(count.incrementAndGet()).divide(new BigDecimal(total), 6, BigDecimal.ROUND_HALF_UP),
  291. num, SceneDownloadProgressStatus.DOWNLOAD_COMPRESSING.code(), null);
  292. }
  293. //写入code.txt
  294. this.zipBytes(out, "code.txt", num.getBytes());
  295. }
  296. private void zipBat(ZipOutputStream out, String num) throws Exception{
  297. String batContent = String.format(this.exeContent, num);
  298. this.zipBytes(out, exeName, batContent.getBytes());
  299. //更新进度为90%
  300. this.updateProgress(new BigDecimal("0.9").divide(new BigDecimal("0.8"), 6, BigDecimal.ROUND_HALF_UP), num,
  301. SceneDownloadProgressStatus.DOWNLOAD_COMPRESSING.code(), null);
  302. }
  303. private Map<String, List<String>> getAllFiles(String num, String v3localPath) throws Exception{
  304. //列出oss所有文件路径
  305. List<String> ossFilePaths = new ArrayList<>();
  306. for (String prefix : prefixArr) {
  307. prefix = String.format(prefix, num);
  308. List<String> keys = uploadToOssUtil.listKeys(prefix);
  309. if(CollUtil.isEmpty(keys)){
  310. continue;
  311. }
  312. if(StorageType.AWS.code().equals(this.uploadType)){
  313. keys = keys.stream().filter(key->{
  314. if(key.contains("x-oss-process")){
  315. return false;
  316. }
  317. return true;
  318. }).collect(Collectors.toList());
  319. }
  320. ossFilePaths.addAll(keys);
  321. }
  322. //列出v3local所有文件路径
  323. File file = new File(v3localPath);
  324. List<String> v3localFilePaths = FileUtils.list(file);
  325. HashMap<String, List<String>> map = new HashMap<>();
  326. map.put("ossFilePaths", ossFilePaths);
  327. map.put("v3localFilePaths", v3localFilePaths);
  328. return map;
  329. }
  330. private JSONObject zipGetInfoJson(ZipOutputStream out, String root, String num) throws Exception{
  331. ResultData<SceneInfoDTO> sceneViewInfo = sceneUserSceneClient.getSceneViewInfo(num);
  332. if(!sceneViewInfo.getSuccess()){
  333. throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
  334. }
  335. SceneInfoDTO data = sceneViewInfo.getData();
  336. JSONObject getInfoJson = null;
  337. if(Objects.isNull(data)){
  338. getInfoJson = new JSONObject();
  339. }else {
  340. getInfoJson = JSONUtil.parseObj(data);
  341. }
  342. getInfoJson.set("sceneScheme", 3);
  343. getInfoJson.set("needKey", 0);
  344. getInfoJson.set("sceneKey","");
  345. //写入getInfo.json
  346. String getInfoJsonPath = root + String.format(UploadFilePath.DATA_VIEW_PATH, num) + "getInfo.json";
  347. this.zipBytes(out, getInfoJsonPath, getInfoJson.toString().getBytes());
  348. return getInfoJson;
  349. }
  350. private void zipSceneJson(ZipOutputStream out, String root, String num, JSONObject sceneJson) throws Exception{
  351. //访问密码置0
  352. JSONObject controls = sceneJson.getJSONObject("controls");
  353. controls.set("showLock", 0);
  354. String sceneJsonPath = root + String.format(UploadFilePath.DATA_VIEW_PATH, num) + "scene.json";
  355. this.zipBytes(out, sceneJsonPath, sceneJson.toString().getBytes());
  356. }
  357. private void processImage(String key, ZipOutputStream out, String resolution, int imagesVersion, Set<String> imgKeys) throws Exception{
  358. if(key.contains("x-oss-process") || key.endsWith("/")){
  359. return;
  360. }
  361. String fileName = key.substring(key.lastIndexOf("/")+1, key.indexOf("."));
  362. String ext = key.substring(key.lastIndexOf("."));
  363. String[] arr = fileName.split("_skybox");
  364. String dir = arr[0];
  365. String num = arr[1];
  366. if(StrUtil.isEmpty(fileName)
  367. || StrUtil.isEmpty(ext)
  368. || (".jpg".equals(ext) && ".png".equals(ext))
  369. || StrUtil.isEmpty(dir)
  370. || StrUtil.isEmpty(num)){
  371. throw new Exception("本地下载图片资源不符合规则,key:" + key);
  372. }
  373. for (ImageType imageType : imageTypes) {
  374. List<ImageTypeDetail> items = Lists.newArrayList();
  375. String[] ranges = imageType.getRanges();
  376. for(int i = 0; i < ranges.length; i++){
  377. String x = ranges[i];
  378. for(int j = 0; j < ranges.length; j++){
  379. String y = ranges[j];
  380. items.add(
  381. ImageTypeDetail.builder()
  382. .i(String.valueOf(i))
  383. .j(String.valueOf(j))
  384. .x(x)
  385. .y(y)
  386. .build()
  387. );
  388. }
  389. }
  390. for (ImageTypeDetail item : items) {
  391. String par = "?x-oss-process=image/resize,m_lfit,w_" + imageType.getSize() + "/crop,w_512,h_512,x_" + item.getX() + ",y_" + item.getY();
  392. if(StorageType.AWS.code().equals(uploadType)){
  393. par += "&imagesVersion="+ imagesVersion;
  394. }
  395. var url = this.
  396. resourceUrl + key;
  397. StorageType storageType = StorageType.get(uploadType);
  398. switch (storageType){
  399. case OSS:
  400. url += par;
  401. break;
  402. case AWS:
  403. url += URLEncoder.encode(par.replace("/", "@"), "UTF-8");
  404. break;
  405. }
  406. var fky = key.split("/" + resolution + "/")[0] + "/" + dir + "/" + imageType.getName() + num + "_" + item.getI() + "_" + item.getJ() + ext;
  407. if(imgKeys.contains(fky)){
  408. continue;
  409. }
  410. imgKeys.add(fky);
  411. this.zipBytes(out, wwwroot + fky, FileUtils.getBytesFromUrl(url));
  412. }
  413. }
  414. }
  415. public void ProcessFiles(String num, String key, ZipOutputStream out, String prefix, Set<String> cacheKeys) throws Exception{
  416. if(cacheKeys.contains(key)){
  417. return;
  418. }
  419. if(key.equals(String.format(UploadFilePath.DATA_VIEW_PATH, num) + "scene.json")){
  420. return;
  421. }
  422. cacheKeys.add(key);
  423. String fileName = key.substring(key.lastIndexOf("/") + 1);
  424. String url = this.resourceUrl + key.replace(fileName, URLEncoder.encode(fileName, "UTF-8")) + "?t=" + Calendar.getInstance().getTimeInMillis();
  425. if(key.contains("hot.json") || key.contains("link-scene.json")){
  426. String content = FileUtils.getStringFromUrl(url);
  427. content.replace(publicUrl, "")
  428. // .replace(publicUrl+"v3/", "")
  429. .replace("https://spc.html","spc.html")
  430. .replace("https://smobile.html", "smobile.html");
  431. zipBytes(out, prefix + key, content.getBytes());
  432. }else{
  433. try {
  434. zipBytes(out, prefix + key, FileUtils.getBytesFromUrl(url));
  435. }catch (Exception e){
  436. log.error("获取字节数据出错,url:{}", url);
  437. }
  438. }
  439. }
  440. public void updateProgress(BigDecimal precent, String num, Integer status, String url){
  441. SceneDownloadProgressStatus progressStatus = SceneDownloadProgressStatus.get(status);
  442. switch (progressStatus){
  443. case DOWNLOAD_SUCCESS:
  444. precent = new BigDecimal("100");
  445. break;
  446. case DOWNLOAD_FAILED:
  447. precent = new BigDecimal("0");
  448. break;
  449. default:
  450. precent = precent.multiply(new BigDecimal("0.8")).multiply(new BigDecimal("100"));
  451. }
  452. DownLoadProgressBean progress = null;
  453. String key = String.format(RedisKey.PREFIX_DOWNLOAD_PROGRESS_V4, num);
  454. String progressStr = redisUtil.get(key);
  455. if(StrUtil.isEmpty(progressStr)){
  456. progress = DownLoadProgressBean.builder().percent(precent.intValue()).status(status).url(url).build();
  457. }else{
  458. progress = JSONUtil.toBean(progressStr, DownLoadProgressBean.class);
  459. //如果下载失败,进度不变
  460. if(status == SceneDownloadProgressStatus.DOWNLOAD_FAILED.code() && progress.getPercent() != null){
  461. precent = new BigDecimal(progress.getPercent());
  462. }
  463. progress.setPercent(precent.intValue());
  464. progress.setStatus(status);
  465. progress.setUrl(url);
  466. }
  467. if(status == 1003){
  468. log.info(JSONUtil.toJsonStr(progress));
  469. }
  470. redisUtil.set(key, JSONUtil.toJsonStr(progress));
  471. }
  472. public void zipInputStream(ZipOutputStream out, String key, FileInputStream in) throws Exception {
  473. out.putNextEntry(new org.apache.tools.zip.ZipEntry(key));
  474. byte[] bytes = new byte[1024];
  475. int b = 0;
  476. while ((b = in.read(bytes)) != -1) {
  477. out.write(bytes, 0, b);
  478. }
  479. }
  480. public synchronized void zipBytes(ZipOutputStream out, String key, byte[] bytes) throws Exception {
  481. out.putNextEntry(new org.apache.tools.zip.ZipEntry(key));
  482. out.write(bytes);
  483. }
  484. }