麻烦大家帮忙看看,为什么下面的程序压缩.txt文件的时候没有问题,但是如果是压缩xls或者是其他
复杂文件(比如:mp3),解压缩之后,原来的文件就不能打开,也就是说文件已经损毁了,麻烦大家帮忙看看!/*
* FileName:ZipDemo.java
* 作成日: Dec 25, 2005
* For Divine Mind! Only fun!
*
* Author: YaoGao
*/
package divinemind.onlyfun;import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;/**
* @author YaoGao
*
* For Divine Mind!
*/
public class ZipDemo { /**
* @param args
*/
public static void main(String[] args) {
ZipDemo zip = new ZipDemo(); try {
zip.compress();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* @throws IOException
*
*/
public void compress() throws IOException {
String outputFileName = "D:\\Zip\\hello.zip";
String inputFileName = "D:\\Zip\\test.xls"; FileOutputStream fout = new FileOutputStream(outputFileName); CheckedOutputStream csum = new CheckedOutputStream(fout, new CRC32()); ZipOutputStream zos = new ZipOutputStream(fout);
zos.setMethod(ZipOutputStream.DEFLATED); BufferedOutputStream out = new BufferedOutputStream(zos); BufferedReader in = new BufferedReader(new FileReader(inputFileName)); zos.putNextEntry(new ZipEntry(outputFileName)); int data;
while ((data = in.read()) != -1) {
out.write(data);
} in.close(); out.close();
}
}
复杂文件(比如:mp3),解压缩之后,原来的文件就不能打开,也就是说文件已经损毁了,麻烦大家帮忙看看!/*
* FileName:ZipDemo.java
* 作成日: Dec 25, 2005
* For Divine Mind! Only fun!
*
* Author: YaoGao
*/
package divinemind.onlyfun;import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;/**
* @author YaoGao
*
* For Divine Mind!
*/
public class ZipDemo { /**
* @param args
*/
public static void main(String[] args) {
ZipDemo zip = new ZipDemo(); try {
zip.compress();
} catch (IOException e) {
e.printStackTrace();
}
} /**
* @throws IOException
*
*/
public void compress() throws IOException {
String outputFileName = "D:\\Zip\\hello.zip";
String inputFileName = "D:\\Zip\\test.xls"; FileOutputStream fout = new FileOutputStream(outputFileName); CheckedOutputStream csum = new CheckedOutputStream(fout, new CRC32()); ZipOutputStream zos = new ZipOutputStream(fout);
zos.setMethod(ZipOutputStream.DEFLATED); BufferedOutputStream out = new BufferedOutputStream(zos); BufferedReader in = new BufferedReader(new FileReader(inputFileName)); zos.putNextEntry(new ZipEntry(outputFileName)); int data;
while ((data = in.read()) != -1) {
out.write(data);
} in.close(); out.close();
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货