【Java】利用ant外掛程式壓縮檔夾及其所有子檔案與子檔案夾

來源:互聯網
上載者:User

標籤:java   壓縮   ant   apache   file   

如果在Java要壓縮一個檔案夾及其所有子檔案與子檔案夾,可以利用到Apache公司提供的ant外掛程式。其實也就是一個jar包。

比如,如果要把f:\bb下的所有檔案,壓縮成一個f:\bb.zip,如:


首先先到Apache的官網,下載ant外掛程式,地址:http://ant.apache.org/bindownload.cgi(點擊開啟連結)


下載解壓之後,取走其中的apache-ant-1.9.4\lib下的ant.jar放到你的java工程就行:


比如拷貝到你的java工程,目錄結構如,在Eclipse右鍵你的java工程properties->java build path->add jars選擇你剛才拷貝過來的ant.jar:


之後,首先在開頭引入如下的包:

package zipTest;import java.io.*;import java.util.*;import org.apache.tools.zip.*;

接著如《【Java】讀取其下所有檔案夾與檔案的路徑》(點擊開啟連結)一樣,先讀取你要壓縮的檔案夾的所有檔案路徑,用一個ArrayList<String>存起來,然後如《【Java】ArrayList<String>轉化為String數組問題》(點擊開啟連結)將其轉化成String數組

public static void main(String[] args) throws Exception {//先讀取f:/bb下的所有檔案的路徑ArrayList<String> dirStrArr = new ArrayList<String>();File dir = new File("f:/bb");if (dir.exists()) {File files[] = dir.listFiles();for (File file : files) {dirStrArr.add(dir.getPath() + "/" + file.getName());}}//輸出為f:/bb.zip,接著要求要求剛才讀取到所有檔案compress(new FileOutputStream("f:/bb.zip"),(String[]) dirStrArr.toArray(new String[0]));}
再通過如下的Java方法,將其壓縮:

//如果是檔案夾,則迴圈遞迴這個方法,因為還要讀取旗下的所有子檔案夾與子檔案。private static void zipDirectory(ZipOutputStream zos, String dirName,String basePath) throws Exception {File dir = new File(dirName);if (dir.exists()) {File files[] = dir.listFiles();if (files.length > 0) {for (File file : files) {//讀取的時候同樣分兩種情況,檔案、檔案夾if (file.isDirectory()) {zipDirectory(zos,file.getPath(),basePath+ file.getName().substring(file.getName().lastIndexOf(File.separator) + 1)+ File.separator);} elsezipFile(zos, file.getPath(), basePath);}//如果是一個空檔案夾} else {//直接壓縮就可以了ZipEntry ze = new ZipEntry(basePath);zos.putNextEntry(ze);}}}private static void zipFile(ZipOutputStream zos, String filename,String basePath) throws Exception {//如果是檔案,應該利用輸入輸出資料流,分段壓縮File file = new File(filename);if (file.exists()) {FileInputStream fis = new FileInputStream(filename);ZipEntry ze = new ZipEntry(basePath + file.getName());zos.putNextEntry(ze);byte[] buffer = new byte[8192];int count = 0;while ((count = fis.read(buffer)) > 0) {zos.write(buffer, 0, count);}fis.close();}}public static void compress(OutputStream os, String... paths)throws Exception {// 指定輸出的地方就是傳過來的osZipOutputStream zos = new ZipOutputStream(os);// 對傳遞過來的路徑進行遍曆for (String path : paths) {// 如果傳遞過來的路徑為空白,則不進行這次迴圈,以免出錯if (path.equals(""))continue;File file = new File(path);// 之後,如果這個路徑存在,則分兩種情況,一種是檔案夾,另一種是檔案if (file.exists()) {if (file.isDirectory()) {zipDirectory(zos, file.getPath(), file.getName()+ File.separator);} else {zipFile(zos, file.getPath(), "");}}}zos.close();}

整個檔案連起來是這樣的:

package zipTest;import java.io.*;import java.util.*;import org.apache.tools.zip.*;public class zipTest {// 如果是檔案夾,則迴圈遞迴這個方法,因為還要讀取旗下的所有子檔案夾與子檔案。private static void zipDirectory(ZipOutputStream zos, String dirName,String basePath) throws Exception {File dir = new File(dirName);if (dir.exists()) {File files[] = dir.listFiles();if (files.length > 0) {for (File file : files) {// 讀取的時候同樣分兩種情況,檔案、檔案夾if (file.isDirectory()) {zipDirectory(zos,file.getPath(),basePath+ file.getName().substring(file.getName().lastIndexOf(File.separator) + 1)+ File.separator);} elsezipFile(zos, file.getPath(), basePath);}// 如果是一個空檔案夾} else {// 直接壓縮就可以了ZipEntry ze = new ZipEntry(basePath);zos.putNextEntry(ze);}}}private static void zipFile(ZipOutputStream zos, String filename,String basePath) throws Exception {// 如果是檔案,應該利用輸入輸出資料流,分段壓縮File file = new File(filename);if (file.exists()) {FileInputStream fis = new FileInputStream(filename);ZipEntry ze = new ZipEntry(basePath + file.getName());zos.putNextEntry(ze);byte[] buffer = new byte[8192];int count = 0;while ((count = fis.read(buffer)) > 0) {zos.write(buffer, 0, count);}fis.close();}}public static void compress(OutputStream os, String... paths)throws Exception {// 指定輸出的地方就是傳過來的osZipOutputStream zos = new ZipOutputStream(os);// 對傳遞過來的路徑進行遍曆for (String path : paths) {// 如果傳遞過來的路徑為空白,則不進行這次迴圈,以免出錯if (path.equals(""))continue;File file = new File(path);// 之後,如果這個路徑存在,則分兩種情況,一種是檔案夾,另一種是檔案if (file.exists()) {if (file.isDirectory()) {zipDirectory(zos, file.getPath(), file.getName()+ File.separator);} else {zipFile(zos, file.getPath(), "");}}}zos.close();}public static void main(String[] args) throws Exception {// 先讀取f:/bb下的所有檔案的路徑ArrayList<String> dirStrArr = new ArrayList<String>();File dir = new File("f:/bb");if (dir.exists()) {File files[] = dir.listFiles();for (File file : files) {dirStrArr.add(dir.getPath() + "/" + file.getName());}}// 輸出為f:/bb.zip,接著要求要求剛才讀取到所有檔案compress(new FileOutputStream("f:/bb.zip"),(String[]) dirStrArr.toArray(new String[0]));}}

【Java】利用ant外掛程式壓縮檔夾及其所有子檔案與子檔案夾

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.