日期:2014-05-19  浏览次数:20870 次

java实现zip压缩后的压缩文件无内容怎么办???
Java code

package toTxt;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

public class ToZip extends MD5Utils{
    /**
     * 对文件进行压缩
     * @param out
     * @param file
     * @param base
     * @throws Exception
     */
    public void zip(ZipOutputStream out, File file, String base) throws Exception{
        System.out.println("正在压缩文档"+file.getName());
    
        ZipEntry entry = new ZipEntry(base);
        out.putNextEntry(entry);
        FileInputStream in = new FileInputStream(file);
        byte[] buffer = new byte[2048];//定义缓冲区
        int num;
        while((num = in.read()) != -1){
            out.write(buffer,0,num);
        }
        System.out.println("压缩成功!");
        in.close();
        
    }
    /**
     * 读入文件和路径
     * @param inputFileName 文件名
     * @param zipFileName  路径名
     * @throws Exception
     */
    public void zip(String inputFileName, String zipFileName) throws Exception{
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
        System.out.println("载入文件成功,正在压缩....");
        zip(out,new File(inputFileName),"");
        out.close();
    }
    

    public static void zipper() throws Exception {
        ToZip zip = new ToZip();
        String inputFileName = path+name;
        System.out.println(info);
        System.out.println("开始对文件"+inputFileName+"进行加密");
        String md5 = MD5Utils.getTxtMD5(new File(path+name));
        String zipFileName = path+md5+".zip";
        System.out.println("加密成功,获得密文:"+"\t"+md5);
        System.out.println(info);
        System.out.println("开始对文件"+inputFileName+"进行加密压缩");
        boolean flag = new File(zipFileName).exists();
        if(flag){
            System.out.println("压缩包"+zipFileName+".zip"+"已存在,取消当前压缩操作!");
        }else{
            zip.zip(inputFileName, zipFileName);
        }
    }
}

客户要求数据生成txt文档,然后进行MD5加密后,最后以密码.zip进行压缩
我压缩之后打开zip查看里面没有内容
求大虾看看什么问题。

------解决方案--------------------
Java code

import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;


/**
     * 压缩文件
     * @param filePaths 存放文件物理路径的集合
     * @param fileNames 文件的名称(和文件路径对应,可以是中文)
     * @param outPath 压缩文件的输出路径(物理路径)
     */
    public void reduceFile(List<String> filePaths, List<String> fileNames, String outPath) {
        if (null != filePaths && filePaths.size() > 0 && null != fileNames && fileNames.size()== filePaths.size()) {
            try {
                OutputStream fileOutput = new FileOutputStream(outPath);
                ZipOutputStream zipOutput = new ZipOutputStream(fileOutput);
                for (int i = 0; i < filePaths.size(); i++) {
                    File file = new File(filePaths.get(i));
                    if (file.exists() && !file.isDirectory()) {
                        InputStream input = new FileInputStream(file);
                        ZipEntry entry = new ZipEntry(fileNames.get(i));
                        zipOutput.putNextEntry(entry);
                        int length = 0;
                        while ((length = input.read()) != -1) {
                            zipOutput.write(length);