java解压文件

摘要

下面实现的功能是zip文件中的图像文件解压到当前目录下,用jdk自带的处理zip文件的代码处理的,但是不能处理中文名称的文件,要不然就会出错。

package cn.ccb.odsbsx.common.util;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
 * 解压功能
 * @author xgliu
 * @date 20100825
 *
 */
public class UnZipApp {
    public static UnZipApp instance = null;
    /**
     * 构造函数
     *
     * @param from
     * @param to
     */
    private UnZipApp() {
    }
       
    /**
     * 获取实例对象
     * @param from
     * @param to
     * @return
     */
    public static UnZipApp getInstance() {
        if (instance == null) {
            synchronized (UnZipApp.class) {
                if (instance == null)
                    instance = new UnZipApp();
            }
        }
        return instance;
    }
    /**
     * 解压功能
     *
     * @param from 压缩文件绝对路径
     * @param to 解压目录
     */
    public boolean unZip(InputStream is, String to) {
        try {
            ZipInputStream zis = new ZipInputStream(is);
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null) {
                if (!entry.isDirectory()) {
                    File myFile = new File(entry.getName());
                    FileOutputStream fout = new FileOutputStream(to
                            + myFile.getPath());
                    DataOutputStream dout = new DataOutputStream(fout);
                    byte[] b = new byte[1024];
                    int len = 0;
                    while ((len = zis.read(b)) != -1) {
                        dout.write(b, 0, len);
                    }
                    dout.close();
                    fout.close();
                }
                zis.closeEntry();
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // 解压缩的文件名
        //String from = "/home/ap/ods/tmp/excel/zip/DEP_SAE_CB_ACCT.zip";
        //String to = "/home/ap/ods/tmp/excel/from/";
        //File file = new File(from);
        //InputStream is = new FileInputStream(file);
        //UnZipApp.getInstance().unZip(is, to);
    }
}


IT家园
IT家园

网友最新评论 (0)