Java压缩zip
@GetMapping ( "/downloadZip" ) @ApiOperation ( value = "下载压缩包" ) @ApiImplicitParam ( name = "instId" , value = "实例id" , required = true , paramType = "query" , dataTypeClass = String . class ) public void downloadZip ( @RequestParam ( "instId" ) String instId, HttpServletResponse response) { Asserts . isEmpty ( instId, "实例id[instId]不能为空" ) ; InputStream inputStream = null ; OutputStream outputStream = null ; try { businessContentService. zipAttachTree ( instId, response) ; outputStream. flush ( ) ; } catch ( Exception e) { response. setStatus ( SC_BAD_REQUEST ) ; try { if ( outputStream != null ) { response. setCharacterEncoding ( "utf-8" ) ; response. setHeader ( "Content-Type" , "application/json;charset=utf-8" ) ; outputStream. write ( JSON . toJSONString ( R . fail ( e. getMessage ( ) ) ) . getBytes ( ) ) ; } } catch ( IOException ex) { throw new RuntimeException ( ex) ; } } finally { try { if ( outputStream != null ) { outputStream. close ( ) ; } } catch ( IOException ex) { throw new RuntimeException ( ex) ; } try { if ( inputStream != null ) { inputStream. close ( ) ; } } catch ( IOException ex) { throw new RuntimeException ( ex) ; } } }
@Override public void zipAttachTree ( String instId, HttpServletResponse response) { InstBusinessInfo instBusinessInfo = instBusinessInfoMapper. selectById ( instId) ; String instTitle = instBusinessInfo. getInstTitle ( ) ; if ( instBusinessInfo == null ) { throw new BusinessException ( instId) ; } BusinessContentRequest instanceContentRequest = new BusinessContentRequest ( ) ; BusinessContentResponse contentResponse = null ; instanceContentRequest. setInstId ( instId) ; instanceContentRequest. setGroupEnum ( BusinessContentGroupEnum . DIR_TREE ) ; try { contentResponse = this . getBusinessContent ( instanceContentRequest) ; } catch ( Exception e) { e. printStackTrace ( ) ; } List < BusinessContentVO > contentVOList = contentResponse. getContentVOList ( ) ; File directory = new File ( "./" + instTitle) ; directory. mkdir ( ) ; this . getFileName ( contentVOList, new StringBuilder ( ) , instTitle) ; File file = new File ( "./" + instTitle) ; File zipFile = null ; List < File > souceFileList = new ArrayList ( ) ; try { souceFileList. add ( file) ; zipFile = new File ( instTitle + ".zip" ) ; ZipUtil . toZip ( zipFile. getName ( ) , souceFileList) ; BufferedInputStream fis = new BufferedInputStream ( new FileInputStream ( zipFile) ) ; byte [ ] buffer = new byte [ fis. available ( ) ] ; fis. read ( buffer) ; fis. close ( ) ; response. reset ( ) ; response. setHeader ( "Content-Disposition" , "attachment;filename=" + URLEncoder . encode ( zipFile. getName ( ) , "UTF-8" ) ) ; OutputStream toClient = new BufferedOutputStream ( response. getOutputStream ( ) ) ; toClient. write ( buffer) ; toClient. flush ( ) ; toClient. close ( ) ; } catch ( Exception e) { e. printStackTrace ( ) ; } finally { if ( file!= null && file. exists ( ) ) { FileUtil . del ( file) ; } if ( zipFile != null && zipFile. exists ( ) ) { zipFile. delete ( ) ; } } } public void getFileName ( List < BusinessContentVO > contentVOList, StringBuilder sb, String instTitle) { List < StringBuilder > fileNames = new ArrayList < > ( ) ; for ( BusinessContentVO contentVO : contentVOList) { OutputStream outputStream = null ; StringBuilder newSb = new StringBuilder ( sb) ; if ( Func . isEmpty ( contentVO. getDataId ( ) ) ) { File directory = new File ( "./" + instTitle + "/" + contentVO. getNodeName ( ) ) ; directory. mkdir ( ) ; newSb. append ( contentVO. getNodeName ( ) + "/" ) ; } else { String dataId = contentVO. getDataId ( ) ; InstFileInfo instFileInfo = instFileInfoMapper. selectById ( dataId) ; InputStream inputStream = minioTemplate. getObject ( instFileInfo. getFilePath ( ) ) ; String fileName = "./" + instTitle + "/" + sb + "/" + contentVO. getNodeName ( ) + "" ; File outputFile = new File ( fileName) ; try { outputStream = new FileOutputStream ( outputFile) ; byte [ ] buffer = new byte [ 1024 ] ; int length; while ( ( length = inputStream. read ( buffer) ) != - 1 ) { outputStream. write ( buffer, 0 , length) ; } inputStream. close ( ) ; outputStream. close ( ) ; } catch ( Exception e) { e. printStackTrace ( ) ; } } List < BusinessContentVO > childrenList = contentVO. getChildren ( ) ; if ( childrenList != null ) { getFileName ( childrenList, newSb, instTitle) ; } } }
将流写到文件中
MultipartFile fileInputStream is = file. getInputStream ( ) ; ZipInputStream zipInputStream = new ZipInputStream ( is, Charset . forName ( "UTF-8" ) ) ; File jsonFile = new File ( "./pre/" + zipEntryNameStr) ; this . writeFile ( jsonFile. getAbsolutePath ( ) , zipInputStream) ; public void writeFile ( String filePath, ZipInputStream zipInputStream) { try ( OutputStream outputStream = new FileOutputStream ( filePath) ) { byte [ ] bytes = new byte [ 4096 ] ; int len; while ( ( len = zipInputStream. read ( bytes) ) != - 1 ) { outputStream. write ( bytes, 0 , len) ; } } catch ( IOException ex) { System . out. println ( "解压文件时,写出到文件出错" ) ; } }