FileCompression[zip].txt
上传用户:sbftbdw
上传日期:2022-01-12
资源大小:1k
文件大小:1k
- /**
- * 把一个文件压缩成zip格式 入口格式: File src = new File("d:/temp/1.txt"); String des
- * ="d:/temp/www.zip";
- *
- * @param src
- * @param des
- */
- public void compress(File src, String des) {
- DataInputStream in = null;
- ZipOutputStream out = null;
- try {
- in = new DataInputStream(new FileInputStream(src));
- out = new ZipOutputStream(new FileOutputStream(des));
- out.putNextEntry(new ZipEntry(src.getName()));
- int n = 0;
- byte[] buff = new byte[1024 * 1024];
- while ((n = in.read(buff)) != -1) {
- out.write(buff, 0, n);
- }
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- try {
- out.close();
- in.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }