Y(^_^)Y
2011-04-20 14:20
浏览 313
已采纳

java 压缩完了以后,原文件消失问题…………

package cn.com.taoge.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;

import org.apache.tools.zip.ZipOutputStream;

public class ZipUtil {

/**
* 放弃,本代码压缩的时候会剪切文件。
* @param fileNames
*            the file name and directory information
*/
public static synchronized boolean zipFile(String[] fileNames) {
FileInputStream fis;
File zipFile = new File("D:sb.zip");
File file;
try {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
zipFile));
for (int i = 0; i < fileNames.length; i++) {
int len;
file = new File(fileNames[i]);
fis = new FileInputStream(file);
zos.putNextEntry((org.apache.tools.zip.ZipEntry) new ZipEntry(fileNames[i]));
while ((len = fis.read()) != -1) {
zos.write(len);
}
fis.close();
file.delete();
}
zos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}

public static void main(String[] args) {
String[] strArray = { "E:/test/俯视.fla", "E:/aaa/片头.fla" };

zipFile(strArray);
}

}

能不能改成让源文件不消失的……急用

 

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • aiyust070 2011-04-20 15:23
    已采纳

    哥们儿,我给你改了改,你在看下:
    [code="java"]
    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.zip.CRC32;
    import java.util.zip.CheckedOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;

    public class ZipCompressor {

    static final int BUFFER = 8192;
    
    private File zipFile;
    private FileOutputStream fileOutputStream;
    private CheckedOutputStream cos;
    private ZipOutputStream out;
    
    /**
     * 构造函数
     * @param pathName
     */
    public ZipCompressor(String pathName) {
        zipFile = new File(pathName);
        try {
            fileOutputStream = new FileOutputStream(zipFile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        cos = new CheckedOutputStream(fileOutputStream, new CRC32());
        out = new ZipOutputStream(cos);
    }
    private void destroy (){
    
        try {
            out.close();
            cos.close();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
    }
    
    /**
     * 向压缩包中添加一个文件或目录
     * @param srcPathName
     */
    public void compress(String srcPathName) {
        File file = new File(srcPathName);
        if (!file.exists())
            throw new RuntimeException(srcPathName + "不存在!");
        try {
            //FileOutputStream fileOutputStream = new FileOutputStream(zipFile);
            //CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32());
            //ZipOutputStream out = new ZipOutputStream(cos);
    
            String basedir = "";
            compress(file, this.out, basedir);
    
    
    
            //out.close();
            //cos.close();
            //fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            //throw new RuntimeException(e);
        }
    }
    
    
    private void compress(File file, ZipOutputStream out, String basedir) {
        /* 判断是目录还是文件 */
        if (file.isDirectory()) {
            System.out.println("压缩:" + basedir + file.getName());
            this.compressDirectory(file, out, basedir);
        } else {
            System.out.println("压缩:" + basedir + file.getName());
            this.compressFile(file, out, basedir);
        }
    }
    
    /** 压缩一个目录 */
    private void compressDirectory(File dir, ZipOutputStream out, String basedir) {
        if (!dir.exists())
            return;
    
        File[] files = dir.listFiles();
        for (int i = 0; i < files.length; i++) {
            // 递归
            compress(files[i], out, basedir + dir.getName() + "/");
        }
        /*
         * File[] files = dir.listFiles(); for (int i = 0; i < files.length;
         * i++) { // 递归 compress(files[i], out, basedir + dir.getName() + "/");
         * }
         */
    }
    
    /** 压缩一个文件 */
    private void compressFile(File file, ZipOutputStream out, String basedir) {
    
        if (!file.exists()) {
            return;
        }
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            ZipEntry entry = new ZipEntry(basedir + file.getName());
            out.putNextEntry(entry);
            int count;
            byte data[] = new byte[BUFFER];
            while ((count = bis.read(data, 0, BUFFER)) != -1) {
                this.out.write(data, 0, count);
            }
            bis.close();
            out.closeEntry();
            //out.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    /** 測試是否可以壓縮 */
    public static void main(String[] args) {
        ZipCompressor zc = new ZipCompressor("E:/test/szhzip.zip");
        String[] aa = { "E:/test/a.cs", "E:/test/b.cs", "E:/test/c.cs" };
        for (int i = 0; i < aa.length; i++) {
            zc.compress(aa[i]);
        }
        zc.destroy();
        System.out.println("OK....");
    
    }
    

    }

    [/code]

    点赞 评论
  • OPPPPOP 2011-04-20 14:49

    [code="java"]
    fis.close();
    //file.delete();
    [/code]

    点赞 评论
  • iteye_14910 2011-04-20 22:59

    用过一个非主流的压缩方式,用java调用linux的gzip,来实现解压缩

    点赞 评论

相关推荐 更多相似问题