怎么在java中壓縮文件?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
站在用戶(hù)的角度思考問(wèn)題,與客戶(hù)深入溝通,找到永城網(wǎng)站設(shè)計(jì)與永城網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶(hù)體驗(yàn)好的作品,建站類(lèi)型包括:網(wǎng)站制作、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國(guó)際域名空間、網(wǎng)站空間、企業(yè)郵箱。業(yè)務(wù)覆蓋永城地區(qū)。
Java是一門(mén)面向?qū)ο缶幊陶Z(yǔ)言,可以編寫(xiě)桌面應(yīng)用程序、Web應(yīng)用程序、分布式系統(tǒng)和嵌入式系統(tǒng)應(yīng)用程序。
1、ZIP文件格式
[local file header + file data + data descriptor]{1,n} + central directory + end of central directory record 即 [文件頭+文件數(shù)據(jù)+數(shù)據(jù)描述符]{此處可重復(fù)n次}+核心目錄+目錄結(jié)束標(biāo)識(shí) 當(dāng)壓縮包中有多個(gè)文件時(shí),就會(huì)有多個(gè)[文件頭+文件數(shù)據(jù)+數(shù)據(jù)描述符]
2、壓縮和下載步驟
(1)創(chuàng)建壓縮包前準(zhǔn)備
//定義壓縮包存在服務(wù)器的路徑 String path = request.getSession().getServletContext().getRealPath("/WEB-INF/fileTemp"); //創(chuàng)建路徑 File FilePath = new File(path + "/file"); if (!FilePath.exists()) { FilePath.mkdir(); } String path = FilePath.getPath() + "/"; //定義導(dǎo)出壓縮包的名稱(chēng) String title ="問(wèn)價(jià)壓縮包"; //壓縮包格式 String fileNamezip = title + ".zip"; String zipPath = path + fileNamezip; //創(chuàng)建一個(gè)ZIP輸出流并實(shí)例化緩沖區(qū)域 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipPath))); //設(shè)置編碼格式(解決linux出現(xiàn)亂碼) out.setEncoding("gbk"); //定義字節(jié)數(shù)組 byte data[] = new byte[2048]; //獲取文件記錄(獲取文件記錄代碼省略) List FileList =。。。; if (!FileList.isEmpty()) { ExportUtil util = new ExportUtil(title,title, request, response, FilePath.getPath()); }
(2)刪除壓縮包之前的數(shù)據(jù),創(chuàng)建壓縮包
util.startZip(FilePath.getPath());
(3)循環(huán)將需要壓縮的文件放到壓縮包中
for (int i = 0; i < FileList.size(); i++) { fileVo fileVo=FileList.get(i); export(fileVo,request,response,title,FilePath.getPath(),fileName); } ------ public void export(fileVo fileVo, HttpServletRequest request, HttpServletResponse response, String title,String path, String fileName) { FileOutputStream fileOutputStream = null; try { File dirFile = null; int i = fileVo.getName().lastIndexOf("."); if(i!=-1){//存在文件類(lèi)型 fileName1 = fileName1 + "." + (fileVo.getName()).substring(i+1); } boolean bFile = false; String mkdirName = path + File.separatorChar + title; dirFile = new File(mkdirName); if(!dirFile.exists()) { dirFile.getParentFile().mkdirs(); } if (dirFile.isDirectory()) { path = mkdirName + File.separatorChar + fileName1; } else { bFile = dirFile.mkdirs(); } if (bFile) { path = mkdirName + File.separatorChar + fileName1; } fileOutputStream = new FileOutputStream(path.replace("*", "")); String fileName = URLEncoder.encode(fileName1, "UTF-8"); if (fileName.length() > 110) { fileName = new String(fileName1.getBytes("gb2312"), "ISO8859-1"); } response.setHeader("Connection", "close"); response.setHeader("Content-Type", "application/octet-stream"); response.setContentType("application/x-msdownload"); response.setHeader("Content-Disposition", "attachment; filename=\"" + Utf8Util.toUtf8String(fileName) + "\""); //讀取文件流輸出到到另一個(gè)位置 fileVo.getFileIo(fileOutputStream); fileOutputStream.close(); } catch (Exception e) { logger.error("異常:原因如下"+e.getMessage(), e); } finally { try { if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e1) { // TODO Auto-generated catch block logger.error("異常:原因如下"+e1.getMessage(), e1); } } } ------
(4)壓縮完成,關(guān)閉輸出流。
util.entdZip(FilePath.getPath());
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
名稱(chēng)欄目:怎么在java中壓縮文件
標(biāo)題網(wǎng)址:http://chinadenli.net/article34/geodpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站收錄、外貿(mào)建站、App開(kāi)發(fā)、軟件開(kāi)發(fā)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)