java代码创建jar_Java 创建ZIP和JAR文件

[java]代码库import java.util.zip.*;

import java.io.*;

public class ZipIt {

public static void main(String args[]) throws IOException {

if (args.length < 2) {

System.err.println("usage: java ZipIt Zip.zip file1 file2 file3");

System.exit(-1);

}

File zipFile = new File(args[0]);

if (zipFile.exists()) {

System.err.println("Zip file already exists, please try another");

System.exit(-2);

}

FileOutputStream fos = new FileOutputStream(zipFile);

ZipOutputStream zos = new ZipOutputStream(fos);

int bytesRead;

byte[] buffer = new byte[1024];

CRC32 crc = new CRC32();

for (int i=1, n=args.length; i < n; i++) {

String name = args[i];

File file = new File(name);

if (!file.exists()) {

System.err.println("Skipping: " + name);

continue;

}

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

crc.reset();

while ((bytesRead = bis.read(buffer)) != -1) {

crc.update(buffer, 0, bytesRead);

}

bis.close();

// Reset to beginning of input stream

bis = new BufferedInputStream(new FileInputStream(file));

ZipEntry entry = new ZipEntry(name);

entry.setMethod(ZipEntry.STORED);

entry.setCompressedSize(file.length());

entry.setSize(file.length());

entry.setCrc(crc.getValue());

zos.putNextEntry(entry);

while ((bytesRead = bis.read(buffer)) != -1) {

zos.write(buffer, 0, bytesRead);

}

bis.close();

}

zos.close();

}

}//源代码片段来自云代码http://yuncode.net

694748ed64b9390909c0d88230893790.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值