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个回答

哥们儿,我给你改了改,你在看下:
[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]

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

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问