`
zx_code
  • 浏览: 96871 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

java压缩文件

    博客分类:
  • Java
阅读更多
Java压缩文件,采用apache提供的api可以有效解决中文乱码,本次提供的方法都是将文件压缩,暂时没有提供压缩文件夹的相关功能,后续提供

代码如下:

package com.xwtec.util;

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

/*
 * 采用ANT中ant.jar包的类可以解决中文乱码问题
 */
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class ZipFileUtil {

    
    /**
     * 压缩单个文件
     * 
     * @param file 被压缩文件名称
     * @param zipFile 压缩后的文件名称
     * @return
     * @throws IOException
     */
    public static boolean zipSingleFile(String file, String zipFile)
            throws IOException {
        boolean bf = true;
        File f = new File(file);
        if (!f.exists()) {
            System.out.println("文件不存在");
            bf = false;
        } else {
            File ff = new File(zipFile);
            if (!f.exists()) {
                ff.createNewFile();
            }
            // 创建文件输入流对象
            FileInputStream in = new FileInputStream(file);
            // 创建文件输出流对象
            FileOutputStream out = new FileOutputStream(zipFile);
            // 创建ZIP数据输出流对象
            ZipOutputStream zipOut = new ZipOutputStream(out);
            // 得到文件名称
            String fileName = file.substring(file.lastIndexOf('/') + 1, file.length());
            // 创建指向压缩原始文件的入口
            ZipEntry entry = new ZipEntry(fileName);
            zipOut.putNextEntry(entry);
            // 向压缩文件中输出数据
            int number = 0;
            byte[] buffer = new byte[512];
            while ((number = in.read(buffer)) != -1) {
                zipOut.write(buffer, 0, number);
            }
            zipOut.close();
            out.close();
            in.close();
        }
        return bf;
    }

    
    /**
     * 压缩多个文件
     * @param files 被压缩文件名称,数组
     * @param zipfile 压缩后的文件名称
     * @return
     * @throws Exception
     */
    public static boolean zipFiles(String[] files, String zipfile)
            throws Exception {
        boolean bf = true;

        // 根据文件路径构造一个文件实例
        File ff = new File(zipfile);
        // 判断目前文件是否存在,如果不存在,则新建一个
        if (!ff.exists()) {
            ff.createNewFile();
        }
        // 根据文件路径构造一个文件输出流
        FileOutputStream out = new FileOutputStream(zipfile);
        // 传入文件输出流对象,创建ZIP数据输出流对象
        ZipOutputStream zipOut = new ZipOutputStream(out);

        // 循环待压缩的文件列表
        for (int i = 0; i < files.length; i++) {
            File f = new File(files[i]);
            if (!f.exists()) {
                bf = false;
            }
            try {
                // 创建文件输入流对象
                FileInputStream in = new FileInputStream(files[i]);
                // 得到当前文件的文件名称
                String fileName = files[i].substring(
                        files[i].lastIndexOf('/') + 1, files[i].length());
                // 创建指向压缩原始文件的入口
                ZipEntry entry = new ZipEntry(fileName);
                zipOut.putNextEntry(entry);
                // 向压缩文件中输出数据
                int nNumber = 0;
                byte[] buffer = new byte[512];
                while ((nNumber = in.read(buffer)) != -1) {
                    zipOut.write(buffer, 0, nNumber);
                }
                // 关闭创建的流对象
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
                bf = false;
            }
        }
        zipOut.close();
        out.close();
        return bf;
    }
    
}


依赖的jar包如下:

欢迎交流企鹅群:211367604
2
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics