集册 Java实例教程 压缩文件

压缩文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

355
压缩文件


//package com.nowjava;

import java.io.*;

/**
来 自 时代Java
**/

import java.util.zip.ZipEntry;


import java.util.zip.ZipOutputStream;


public class Main {

    public static void zipFile(File inputFile, File outputZip) {

        byte[] buffer = new byte[1024];


        try {

            FileOutputStream fos = new FileOutputStream(outputZip);

            ZipOutputStream zos = new ZipOutputStream(fos);

            ZipEntry ze = new ZipEntry(inputFile.getName());

            zos.putNextEntry(ze);

            FileInputStream in = new FileInputStream(inputFile);/**来 自 N  o w  J a v a . c o m**/


            int len;

            while ((len = in.read(buffer)) > 0) {

                zos.write(buffe
展开阅读全文