java (jdk+ant.jar包)方式实现zip打包。

package com.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
 *
 * jdk+ant.jar包方式实现zip打包。
 * <功能详细描述>
 *
 * @author  Vincent
 * @version  [版本号, 2014-10-10]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class TestZip
{
    private void zip(String souceFileName, String destFileName)
    {
        File file = new File(souceFileName);
        try
        {
            zip(file, destFileName, "");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    private void zip(String souceFileName, String destFileName,String base)
    {
        File file = new File(souceFileName);
        try
        {
            zip(file, destFileName, base);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    private void zip(File souceFile, String destFileName,String base)
        throws IOException
    {
        FileOutputStream fileOut = null;
        try
        {
            fileOut = new FileOutputStream(destFileName);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        ZipOutputStream out = new ZipOutputStream(fileOut);
        zip(souceFile, out, base);
        out.close();
    }
    
    private void zip(File souceFile, ZipOutputStream out, String base)
        throws IOException
    {
        
        if (souceFile.isDirectory())
        {
            File[] files = souceFile.listFiles();
            out.putNextEntry(new ZipEntry(base + "/"));
            base = base.length() == 0 ? "" : base + "/";
            for (File file : files)
            {
                zip(file, out, base + file.getName());
            }
        }
        else
        {
            if (base.length() > 0)
            {
                out.putNextEntry(new ZipEntry(base));
            }
            else
            {
                out.putNextEntry(new ZipEntry(souceFile.getName()));
            }
            System.out.println("filepath=" + souceFile.getPath());
            FileInputStream in = new FileInputStream(souceFile);
            
            int b;
            byte[] by = new byte[1024];
            while ((b = in.read(by)) != -1)
            {
                out.write(by, 0, b);
            }
            in.close();
        }

    }

  /**
     * **************************更快捷的打zip包的方法**************************************************************
     */

    /**
     * 另一种方式打zip包------22 <功能详细描述>
     *
     * @param epubZipDir--源路径
     * @param afterDrmPath ---目标路径
     * @return [参数说明]
     *
     * @return long [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    private long dozip(String epubZipDir, String afterDrmPath)
    {
        long result = 0;
        Zip z = new Zip();
        File parent = new File(epubZipDir);
        Project p = new Project();
        z.setProject(p);
        z.setBasedir(parent);
        z.setDestFile(new File(afterDrmPath));
        z.execute();
        result = new File(afterDrmPath).length();
        return result;
    }
    /**
     * Test
     */
    public static void main(String[] args)
    {
        TestZip z = new TestZip();
//        z.zip("C:/Intel/ExtremeGraphics", "C:/10.zip","CNBJTW0/2014/08/21");
        String fileName="CNBJTW003015109200000386141";
        //z.zip("f:/"+fileName, "f:/zip_file1111.zip");
        z.zip("f:/"+fileName, "d:/zip_file88888.epub");
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值