| « | June 2026 | » | | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | | | | | |
| 公告 |
| 暂无公告... |
| Blog信息 |
|
blog名称:天地无用 日志总数:55 评论数量:43 留言数量:1 访问次数:197934 建立时间:2008年4月17日 |

| |
|
如何用Java压缩文件(转) 软件技术
kkk888929 发表于 2008/4/22 14:56:08 |
|
/** [Demo Program]** try {* zipDir("c:/temp", "c:/temp.zip", true);* } catch(Exception e) {* //handle exception* }*/public static void zipDir(String directory, String zipname, booleanoverride) {try {File file = new File(zipname);if (file.isDirectory())return;if ((!override) && file.exists())return;file.delete();ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));zipDir(directory, zos);DgIOUtils.close(zos);} catch (FileNotFoundException e) {}}protected static boolean zipDir(String directory, ZipOutputStream zos) {try {//create a new File object based on the directory we have to zip FileFile zipDir = new File(directory);//get a listing of the directory contentString[] dirList = zipDir.list();byte[] readBuffer = new byte[2156];int bytesIn = 0;//loop through dirList, and zip the filesfor (int i = 0; i < dirList.length; i++) {File f = new File(zipDir, dirList[i]);if (f.isDirectory()) {//if the File object is a directory, call this//function again to add its content recursivelyString filePath = f.getPath();zipDir(filePath, zos);//loop againcontinue;}//if we reached here, the File object f was not a directory//create a FileInputStream on top of fFileInputStream fis = new FileInputStream(f);//create a new zip entryZipEntry anEntry = new ZipEntry(f.getPath());//place the zip entry in the ZipOutputStream objectzos.putNextEntry(anEntry);//now write the content of the file to the ZipOutputStreamwhile ((bytesIn = fis.read(readBuffer)) != -1) {zos.write(readBuffer, 0, bytesIn);}//close the StreamDgIOUtils.close(fis);return true;}} catch (Exception e) {//handle exception}return false;} |
|
|