這篇文章將為大家詳細(xì)講解有關(guān)java如何實(shí)現(xiàn)文件夾解壓和壓縮,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
為寶塔等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及寶塔網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、成都外貿(mào)網(wǎng)站建設(shè)、寶塔網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專(zhuān)業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
效果
實(shí)現(xiàn)多個(gè)文件以及文件夾的壓縮和解壓
代碼分析
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class Main { public static void main(String[] args) throws IOException { //解決中文亂碼 //壓縮 參數(shù)改成你自己的源文件路徑和壓縮后的文件路徑 //yasuo("C:\\file\\", "C:\\file.zip"); //解壓 參數(shù)改成你自己的源文件路徑和解壓后的文件路徑 jieya("C:\\file.zip", "C:\\file\\"); } public static void jieya(String zipPath, String path) throws IOException, FileNotFoundException { //創(chuàng)建解壓后的文件夾 File pt=new File(path.substring(0,path.length()-1)); if(!pt.exists()) { pt.mkdirs(); } //try(resource)來(lái)保證InputStream正確關(guān)閉 try(ZipInputStream zip=new ZipInputStream(new FileInputStream(zipPath))){ //ZipEntry表示一個(gè)壓縮文件或目錄 ZipEntry entry; while((entry=zip.getNextEntry())!=null) { String name=entry.getName(); //壓縮文件 if(!(entry.getName().contains(File.separator))) { FileOutputStream file= new FileOutputStream( path+ name); int n=0; while((n=zip.read())!=-1) { file.write(n); } }else { //目錄 int index=name.lastIndexOf("\\"); File file= new File(path+ name.substring(0,index)); if(!file.exists()) { file.mkdirs(); } //如果不是空目錄 if(index!=name.length()-1) { FileOutputStream f= new FileOutputStream( path+ name); int n=0; while((n=zip.read())!=-1) { f.write(n); } } } } zip.closeEntry(); } } public static void yasuo(String path, String zipPath) throws IOException, FileNotFoundException { File zp=new File(zipPath); if(!zp.exists()) { zp.createNewFile(); } try(ZipOutputStream zip=new ZipOutputStream(new FileOutputStream(zp))) { File files= new File(path); File[] f=files.listFiles(); for (File file : f) { zipAll(zip, file,file.getName()); } } } public static void zipAll(ZipOutputStream zip, File files,String name) throws IOException, FileNotFoundException { if(files.isDirectory()) { File[] files2=files.listFiles(); if(files2.length==0||files2==null) { zip.putNextEntry(new ZipEntry(name+File.separator)); }else{ for (File file2 : files2) { if(file2.isFile()) { zip.putNextEntry(new ZipEntry(name+File.separator+file2.getName())); int n; FileInputStream input=new FileInputStream(file2); while((n=input.read())!=-1) { zip.write(n); } } else { zipAll(zip,file2,name+File.separator+file2.getName()); } } } }else { zip.putNextEntry(new ZipEntry(name)); int n; FileInputStream input=new FileInputStream(files); while ((n=input.read())!=-1) { zip.write(n); } } } }
關(guān)于“java如何實(shí)現(xiàn)文件夾解壓和壓縮”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
網(wǎng)頁(yè)名稱(chēng):java如何實(shí)現(xiàn)文件夾解壓和壓縮
鏈接地址:http://www.rwnh.cn/article22/jipecc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、用戶體驗(yàn)、手機(jī)網(wǎng)站建設(shè)、、App設(shè)計(jì)、網(wǎng)站維護(hù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(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)