<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    浪跡天涯
    web報表設計器....
    posts - 61,comments - 71,trackbacks - 0

    package codemaking.util;

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.List;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;
    import java.util.zip.ZipOutputStream;
    import java.util.zip.GZIPInputStream;
    import java.io.DataInputStream;

    ?

    /**
    ?* Description: 此類用于...
    ?*
    ?* @author??? wunaigang(2005-6-21)
    ?* @version?? 1.0.0
    ?*/
    public class ZipManager {

    ?

    ??????? /**
    ???????? * zip壓縮功能測試. 將d:\\temp\\zipout目錄下的所有文件連同子目錄壓縮到d:\\temp\\out.zip.
    ???????? *
    ???????? * @param baseDir 所要壓縮的目錄名(包含絕對路徑)
    ???????? * @param objFileName 壓縮后的文件名
    ???????? * @throws Exception
    ???????? */
    ??????? public void createZip(String baseDir, String objFileName) throws Exception {
    ??????????????? File folderObject = new File(baseDir);

    ?

    ??????????????? if (folderObject.exists()){
    ??????????????????????? List fileList = getSubFiles(new File(baseDir));

    ?

    ??????????????????????? //壓縮文件名
    ??????????????????????? ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFileName));

    ?

    ??????????????????????? ZipEntry ze = null;
    ??????????????????????? byte[] buf = new byte[1024];
    ??????????????????????? int readLen = 0;
    ??????????????????????? for (int i = 0; i < fileList.size(); i++) {
    ??????????????????????????????? File f = (File) fileList.get(i);
    ??????????????????????????????? System.out.println("Adding: " + f.getPath() + f.getName());

    ?

    ??????????????????????????????? //創(chuàng)建一個ZipEntry,并設置Name和其它的一些屬性
    ??????????????????????????????? ze = new ZipEntry(getAbsFileName(baseDir, f));
    ??????????????????????????????? ze.setSize(f.length());
    ??????????????????????????????? ze.setTime(f.lastModified());

    ?

    ??????????????????????????????? //將ZipEntry加到zos中,再寫入實際的文件內容
    ??????????????????????????????? zos.putNextEntry(ze);
    ??????????????????????????????? InputStream is = new BufferedInputStream(new FileInputStream(f));
    ??????????????????????????????? while ((readLen = is.read(buf, 0, 1024)) != -1) {
    ??????????????????????????????????????? zos.write(buf, 0, readLen);
    ??????????????????????????????? }
    ??????????????????????????????? is.close();
    ??????????????????????????????? System.out.println("done...");
    ??????????????????????? }
    ??????????????????????? zos.close();
    ??????????????? }else{
    ??????????????????????? throw new Exception("this folder isnot exist!");
    ??????????????? }
    ??????? }
    ??????? /**
    ???????? * zip壓縮功能測試. 將指定文件壓縮后存到一壓縮文件中
    ???????? *
    ???????? * @param baseDir 所要壓縮的文件名
    ???????? * @param objFileName 壓縮后的文件名
    ???????? * @return 壓縮后文件的大小
    ???????? * @throws Exception
    ???????? */
    ??????? public long createFileToZip(String zipFilename,String sourceFileName) throws Exception {

    ?

    ??????????????? File? sourceFile = new File(sourceFileName);

    ?

    ??????????????? byte[] buf = new byte[1024];

    ?

    ??????????????? //壓縮文件名
    ??????????????? File objFile = new File(zipFilename);

    ?

    ??????????????? ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFile));

    ?

    ??????????????? ZipEntry ze = null;
    ??????????????? //創(chuàng)建一個ZipEntry,并設置Name和其它的一些屬性
    ??????????????? ze = new ZipEntry(sourceFile.getName());
    ??????????????? ze.setSize(sourceFile.length());
    ??????????????? ze.setTime(sourceFile.lastModified());

    ?

    ??????????????? //將ZipEntry加到zos中,再寫入實際的文件內容
    ??????????????? zos.putNextEntry(ze);

    ?

    ??????????????? InputStream is = new BufferedInputStream(new FileInputStream(sourceFile));

    ?

    ??????????????? int readLen = -1;
    ??????????????? while ((readLen = is.read(buf, 0, 1024)) != -1) {
    ??????????????????????? zos.write(buf, 0, readLen);
    ??????????????? }
    ??????????????? is.close();
    ??????????????? zos.close();

    ?


    ??????????????? return objFile.length();
    ??????? }
    ??????? /**
    ???????? * zip壓縮功能測試. 將指定文件壓縮后存到一壓縮文件中
    ???????? *
    ???????? * @param baseDir 所要壓縮的文件名
    ???????? * @param objFileName 壓縮后的文件名
    ???????? * @return 壓縮后文件的大小
    ???????? * @throws Exception
    ???????? */
    ??????? public long createFileToZip(File sourceFile,File zipFile)throws IOException {

    ?


    ??????????????? byte[] buf = new byte[1024];

    ?

    ??????????????? //壓縮文件名
    ??????????????? File objFile = zipFile;

    ?

    ??????????????? ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(objFile));

    ?

    ??????????????? ZipEntry ze = null;
    ??????????????? //創(chuàng)建一個ZipEntry,并設置Name和其它的一些屬性
    ??????????????? ze = new ZipEntry(sourceFile.getName());
    ??????????????? ze.setSize(sourceFile.length());
    ??????????????? ze.setTime(sourceFile.lastModified());

    ?

    ??????????????? //將ZipEntry加到zos中,再寫入實際的文件內容
    ??????????????? zos.putNextEntry(ze);

    ?

    ??????????????? InputStream is = new BufferedInputStream(new FileInputStream(sourceFile));

    ?

    ??????????????? int readLen = -1;
    ??????????????? while ((readLen = is.read(buf, 0, 1024)) != -1) {
    ??????????????????????? zos.write(buf, 0, readLen);
    ??????????????? }
    ??????????????? is.close();
    ??????????????? zos.close();

    ?

    ??????????????? return objFile.length();
    ??????? }

    ?

    ??????? /**
    ???????? * 測試解壓縮功能. 將d:\\download\\test.zip連同子目錄解壓到d:\\temp\\zipout目錄下.
    ???????? *
    ???????? * @throws Exception
    ???????? */
    ??????? public void releaseZipToFile(String sourceZip, String outFileName)
    ??????????????????????? throws IOException{
    ????????????????????? ZipFile zfile=new ZipFile(sourceZip);
    ????????????????????? System.out.println(zfile.getName());
    ????????????????????? Enumeration zList=zfile.entries();
    ????????????????????? ZipEntry ze=null;
    ????????????????????? byte[] buf=new byte[1024];
    ????????????????????? while(zList.hasMoreElements()){
    ????????????????????? //從ZipFile中得到一個ZipEntry
    ????????????????????? ze=(ZipEntry)zList.nextElement();
    ????????????????????? if(ze.isDirectory()){
    ????????????????????? continue;
    ????????????????????? }
    ????????????????????? //以ZipEntry為參數(shù)得到一個InputStream,并寫到OutputStream中
    ????????????????????? OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(outFileName, ze.getName())));
    ????????????????????? InputStream is=new BufferedInputStream(zfile.getInputStream(ze));
    ????????????????????? int readLen=0;
    ????????????????????? while ((readLen=is.read(buf, 0, 1024))!=-1) {
    ????????????????????? os.write(buf, 0, readLen);
    ????????????????????? }
    ????????????????????? is.close();
    ????????????????????? os.close();
    ????????????????????? System.out.println("Extracted: "+ze.getName());
    ????????????????????? }
    ????????????????????? zfile.close();

    ??????? }

    ?

    ??????? /**
    ???????? * 取得指定目錄下的所有文件列表,包括子目錄.
    ???????? *
    ???????? * @param baseDir
    ???????? *??????????? File 指定的目錄
    ???????? * @return 包含java.io.File的List
    ???????? */
    ??????? private List getSubFiles(File baseDir) {
    ??????????????? List ret = new ArrayList();
    ??????????????? //File base=new File(baseDir);
    ??????????????? File[] tmp = baseDir.listFiles();
    ??????????????? for (int i = 0; i < tmp.length; i++) {
    ??????????????????????? if (tmp[i].isFile()) {
    ??????????????????????????????? ret.add(tmp[i]);
    ??????????????????????? }
    ??????????????????????? if (tmp[i].isDirectory()) {
    ??????????????????????????????? ret.addAll(getSubFiles(tmp[i]));
    ??????????????????????? }
    ??????????????? }
    ??????????????? return ret;
    ??????? }

    ?

    ??????? /**
    ???????? * 給定根目錄,返回一個相對路徑所對應的實際文件名.
    ???????? *
    ???????? * @param baseDir
    ???????? *??????????? 指定根目錄
    ???????? * @param absFileName
    ???????? *??????????? 相對路徑名,來自于ZipEntry中的name
    ???????? * @return java.io.File 實際的文件
    ???????? */
    ??????? private File getRealFileName(String baseDir, String absFileName) {
    ??????????????? String[] dirs = absFileName.split("/");
    ??????????????? //System.out.println(dirs.length);
    ??????????????? File ret = new File(baseDir);
    ??????????????? //System.out.println(ret);
    ??????????????? if (dirs.length > 1) {
    ??????????????????????? for (int i = 0; i < dirs.length - 1; i++) {
    ??????????????????????????????? ret = new File(ret, dirs[i]);
    ??????????????????????? }
    ??????????????? }
    ??????????????? if (!ret.exists()) {
    ??????????????????????? ret.mkdirs();
    ??????????????? }
    ??????????????? ret = new File(ret, dirs[dirs.length - 1]);
    ??????????????? return ret;
    ??????? }

    ?

    ??????? /**
    ???????? * 給定根目錄,返回另一個文件名的相對路徑,用于zip文件中的路徑.
    ???????? *
    ???????? * @param baseDir
    ???????? *??????????? java.lang.String 根目錄
    ???????? * @param realFileName
    ???????? *??????????? java.io.File 實際的文件名
    ???????? * @return 相對文件名
    ???????? */
    ??????? private String getAbsFileName(String baseDir, File realFileName) {
    ??????????????? File real = realFileName;
    ??????????????? File base = new File(baseDir);
    ??????????????? String ret = real.getName();
    ??????????????? while (true) {
    ??????????????????????? real = real.getParentFile();
    ??????????????????????? if (real == null)
    ??????????????????????????????? break;
    ??????????????????????? if (real.equals(base))
    ??????????????????????????????? break;
    ??????????????????????? else {
    ??????????????????????????????? ret = real.getName() + "/" + ret;
    ??????????????????????? }
    ??????????????? }
    ??????????????? System.out.println("TTTTT" + ret);
    ??????????????? return ret;
    ??????? }


    ??????? public void testReadZip() throws Exception{
    ??????? String baseDir="d:\\temp\\zipout";
    ??????? ZipFile zfile=new ZipFile("d:\\download\\src.zip");
    ??????? System.out.println(zfile.getName());
    ??????? Enumeration zList=zfile.entries();
    ??????? ZipEntry ze=null;
    ??????? byte[] buf=new byte[1024];
    ??????? while(zList.hasMoreElements()){
    ??????? //從ZipFile中得到一個ZipEntry
    ??????? ze=(ZipEntry)zList.nextElement();
    ??????? if(ze.isDirectory()){
    ??????? continue;
    ??????? }
    ??????? //以ZipEntry為參數(shù)得到一個InputStream,并寫到OutputStream中
    ??????? OutputStream os=new BufferedOutputStream(new FileOutputStream(getRealFileName(baseDir, ze.getName())));
    ??????? InputStream is=new BufferedInputStream(zfile.getInputStream(ze));
    ??????? int readLen=0;
    ??????? while ((readLen=is.read(buf, 0, 1024))!=-1) {
    ??????? os.write(buf, 0, readLen);
    ??????? }
    ??????? is.close();
    ??????? os.close();
    ??????? System.out.println("Extracted: "+ze.getName());
    ??????? }
    ??????? zfile.close();
    ??????? }


    ?? public static void main(String args[]){
    ???? ZipManager manager = new ZipManager();
    ???? try {
    ?????? //manager.releaseZipToFile("c:\\test.zip","c:\\test");
    ?????? manager.testReadZip();
    ???? }
    ???? catch (Exception e) {}
    ???? System.out.println("over");
    ?? }
    ?


    }

    posted on 2007-03-25 00:35 JJCEA 閱讀(12946) 評論(4)  編輯  收藏 所屬分類: 學習日記

    FeedBack:
    # re: 利用java的ZipOutputStream類解壓文件或文件夾中的文件
    2008-09-02 13:52 | 請求
    謝謝您這篇文章,但是好像補支持中文文件名啊。  回復  更多評論
      
    # re: 利用java的ZipOutputStream類解壓文件或文件夾中的文件
    2009-02-14 18:37 | Nassir
    吳哥,不錯!呵呵  回復  更多評論
      
    # re: 利用java的ZipOutputStream類解壓文件或文件夾中的文件
    2012-08-20 16:59 | 路人
    很好,不過有點小問題,你的壓縮后中文變亂碼。還有個問題就是你的邏輯有點問題,如果 要壓縮的文件夾/文件夾/文件夾/文件 壓縮后變?yōu)閴嚎s后的文件/文件夾/文件。也就是說 private String getAbsFileName(String baseDir, File realFileName) 這方法里面的代碼寫的不好。  回復  更多評論
      
    # re: 利用java的ZipOutputStream類解壓文件或文件夾中的文件
    2013-06-04 21:08 | twlkyao
    壓縮完了之后文件會損壞。  回復  更多評論
      
    主站蜘蛛池模板: 男女超爽视频免费播放| 国产色婷婷精品免费视频| 特级做a爰片毛片免费看| 亚洲人成激情在线播放| 久久亚洲高清观看| 亚洲人成人网站在线观看| 啦啦啦手机完整免费高清观看| 99在线观看精品免费99| 久久九九久精品国产免费直播 | 在线免费视频你懂的| 亚洲gay片在线gv网站| 亚洲特级aaaaaa毛片| 国产成A人亚洲精V品无码 | 亚洲欧美日韩一区二区三区 | 免费h视频在线观看| 黄色短视频免费看| 色妞www精品视频免费看| 亚洲人成电影网站色www| 国产99在线|亚洲| 免费看搞黄视频网站| 人妖系列免费网站观看| 免费人人潮人人爽一区二区| 亚洲人成网站18禁止| 亚洲综合色丁香婷婷六月图片| 亚洲同性男gay网站在线观看| 亚洲黄色免费电影| 67pao强力打造67194在线午夜亚洲 | 久久成人国产精品免费软件| 无码国产精品一区二区免费3p | 亚洲高清免费视频| 免费中文字幕在线| 亚洲高清免费视频| 久久久久亚洲?V成人无码| 亚洲福利精品一区二区三区| 亚洲精品天堂成人片?V在线播放| 成人亚洲网站www在线观看| 国产日韩成人亚洲丁香婷婷| 国产亚洲精aa成人网站| 亚洲精品乱码久久久久久蜜桃不卡| 亚洲精品亚洲人成在线观看| 亚洲va久久久噜噜噜久久 |