這篇文章主要介紹“Java怎么壓縮和解壓zip文件”,在日常操作中,相信很多人在Java怎么壓縮和解壓zip文件問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java怎么壓縮和解壓zip文件”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)專業(yè)網(wǎng)站設(shè)計、網(wǎng)站制作,集網(wǎng)站策劃、網(wǎng)站設(shè)計、網(wǎng)站制作于一體,網(wǎng)站seo、網(wǎng)站優(yōu)化、網(wǎng)站營銷、軟文推廣等專業(yè)人才根據(jù)搜索規(guī)律編程設(shè)計,讓網(wǎng)站在運(yùn)行后,在搜索中有好的表現(xiàn),專業(yè)設(shè)計制作為您帶來效益的網(wǎng)站!讓網(wǎng)站建設(shè)為您創(chuàng)造效益。
代碼:
public class ZipUtil { /** * 壓縮文件/文件夾 * */ public static void compress(String srcFilePath, String destFilePath) { File src = new File(srcFilePath); if (!src.exists()) { throw new RuntimeException(srcFilePath + "不存在"); } File zipFile = new File(destFilePath); try { FileOutputStream fos = new FileOutputStream(zipFile); CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32()); ZipOutputStream zos = new ZipOutputStream(cos); String baseDir = ""; compressbyType(src, zos, baseDir); zos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void compressbyType(File src, ZipOutputStream zos, String baseDir) { if (!src.exists()) return; System.out.println("壓縮" + baseDir + src.getName()); if (src.isFile()) { compressFile(src, zos, baseDir); } else if (src.isDirectory()) { compressDir(src, zos, baseDir); } } /** * 壓縮文件 */ private static void compressFile(File file, ZipOutputStream zos, String baseDir) { if (!file.exists()) return; try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); ZipEntry entry = new ZipEntry(baseDir + file.getName()); zos.putNextEntry(entry); int count; byte[] buf = new byte[8019]; while ((count = bis.read(buf)) != -1) { zos.write(buf, 0, count); } bis.close(); } catch (Exception e) { // TODO: handle exception } } /** * 壓縮文件夾 * */ private static void compressDir(File dir, ZipOutputStream zos, String baseDir) { if (!dir.exists()) return; File[] files = dir.listFiles(); if(files.length == 0){ try { zos.putNextEntry(new ZipEntry(baseDir + dir.getName() + File.separator)); } catch (IOException e) { e.printStackTrace(); } } for (File file : files) { compressbyType(file, zos, baseDir + dir.getName() + File.separator); } } /** * 解壓文件/文件夾 */ public static void decompress(String srcPath, String dest) throws Exception { File file = new File(srcPath); if (!file.exists()) { throw new RuntimeException(srcPath + "所指文件不存在"); } ZipFile zf = new ZipFile(file); Enumeration entries = zf.entries(); ZipEntry entry = null; while (entries.hasMoreElements()) { entry = (ZipEntry) entries.nextElement(); System.out.println("解壓" + entry.getName()); if (entry.isDirectory()) { String dirPath = dest + File.separator + entry.getName(); File dir = new File(dirPath); dir.mkdirs(); } else { // 表示文件 File f = new File(dest + File.separator + entry.getName()); if (!f.exists()) { //String dirs = FileUtils.getParentPath(f); String dirs = f.getParent(); File parentDir = new File(dirs); parentDir.mkdirs(); } f.createNewFile(); // 將壓縮文件內(nèi)容寫入到這個文件中 InputStream is = zf.getInputStream(entry); FileOutputStream fos = new FileOutputStream(f); int count; byte[] buf = new byte[8192]; while ((count = is.read(buf)) != -1) { fos.write(buf, 0, count); } is.close(); fos.close(); } } } public static void main(String[] args) throws Exception { // compress("/Users/liyinlong/lyl/test","/Users/liyinlong/lyl/test.zip"); decompress("/Users/liyinlong/lyl/test.zip", "/Users/liyinlong/lyl/test"); } }
到此,關(guān)于“Java怎么壓縮和解壓zip文件”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)頁標(biāo)題:Java怎么壓縮和解壓zip文件
轉(zhuǎn)載注明:http://muchs.cn/article44/ihishe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、響應(yīng)式網(wǎng)站、搜索引擎優(yōu)化、域名注冊、電子商務(wù)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)