Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1449520
  • 博文数量: 254
  • 博客积分: 8696
  • 博客等级: 中将
  • 技术积分: 2961
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-03 16:46
文章分类

全部博文(254)

文章存档

2015年(4)

2014年(18)

2013年(16)

2012年(8)

2011年(25)

2010年(2)

2009年(74)

2008年(107)

分类: Java

2008-07-22 20:33:04

@Test
public void testPackageContent()
{
    try
    {
        FileOutputStream f = new FileOutputStream(BASE_PATH + "/../" +packageName + ".zip");


        CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());//Adler32 is faster than CRC32


        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(csum));


        out.setComment("some comments");


        File srcDir = new File(BASE_PATH);


        zipFile(out, srcDir, "packageName");


        out.close();
    }

    catch (IOException e)
    {
        e.printStackTrace();
    }
}

/**
 * zip the given file
 * @param out - ZipOutputStream
 * @param srcDir - if it is a file then add this file into zip, if it is dir, then call zipFile cascade
 * @param filePath - the filepath in zipfile
 * @throws IOException
 */
private void zipFile(ZipOutputStream out, File srcDir, String filePath) throws IOException

{
    for(File file : srcDir.listFiles())
    {
        if(file.isDirectory())
        {
            zipFile(out, file, filePath+"/"+file.getName());


            continue;
        }


        Logger.global.info("Writing file : "+file.getName());


        FileInputStream in = new FileInputStream(file);


        out.putNextEntry(new ZipEntry(filePath+"/"+file.getName()));


        byte[] b = new byte[1024];


        int off = 0;


        while((in.read(b,off,1024)) != -1)
        {
            out.write(b);
        }
        in.close();
    }
}

(上述出自:)
阅读(675) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~