資源描述:
《java實(shí)現(xiàn)壓縮與解壓縮ZIP》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、壓縮的import?java.io.BufferedInputStream;import?java.io.BufferedOutputStream;import?java.io.File;import?java.io.FileInputStream;import?java.io.FileOutputStream;import?java.util.zip.ZipEntry;import?java.util.zip.ZipOutputStream;public?class?Zip?{????static
2、?final?int?BUFFER?=?2048;????public?static?void?main(String?argv[])?{????????try?{????????????BufferedInputStream?origin?=?null;????????????FileOutputStream?dest?=?new?FileOutputStream("E:\test\myfiles.zip");????????????ZipOutputStream?out?=?new?ZipO
3、utputStream(new?BufferedOutputStream(????????????????????dest));????????????byte?data[]?=?new?byte[BUFFER];????????????File?f?=?new?File("e:\test\a\");????????????File?files[]?=?f.listFiles();????????????for?(int?i?=?0;?i?
4、???????????FileInputStream?fi?=?new?FileInputStream(files[i]);????????????????origin?=?new?BufferedInputStream(fi,?BUFFER);????????????????ZipEntry?entry?=?new?ZipEntry(files[i].getName());????????????????out.putNextEntry(entry);????????????????int?cou
5、nt;????????????????while?((count?=?origin.read(data,?0,?BUFFER))?!=?-1)?{????????????????????out.write(data,?0,?count);????????????????}????????????????origin.close();????????????}????????????out.close();????????}?catch?(Exception?e)?{????????????e.pri
6、ntStackTrace();????????}????}}2,解壓縮的。import?java.io.BufferedInputStream;import?java.io.BufferedOutputStream;import?java.io.File;import?java.io.FileOutputStream;import?java.util.Enumeration;import?java.util.zip.ZipEntry;import?java.util.zip.ZipFile;publ
7、ic?class?UnZip?{????static?final?int?BUFFER?=?2048;????public?static?void?main(String?argv[])?{????????try?{????????????String?fileName?=?"E:\test\myfiles.zip";????????????String?filePath?=?"E:\test\";????????????ZipFile?zipFile?=?new?ZipFile(fileN
8、ame);????????????Enumeration?emu?=?zipFile.entries();????????????int?i=0;????????????while(emu.hasMoreElements()){????????????????ZipEntry?entry?=?(ZipEntry)emu.nextElement();????????????????//會(huì)把目錄作為一個(gè)file讀出一次,所以只建立目錄就可以,之下的文件還會(huì)被迭代到。???