由于使用java.util.zip.ZipOutputStream压缩不能解决中文问题
使用了ant.jar包中的ZipOutputStream,但是压缩速度很慢。谢谢各位踊跃发言,呵呵代码如下:FileOutputStream fout = null;
BufferedOutputStream bos = null;
try {
fout = new FileOutputStream(path);
bos = new BufferedOutputStream(fout);
CheckedOutputStream csum = new CheckedOutputStream(bos, new Adler32());
zip = new ZipOutputStream(csum);
zip.setLevel(Deflater.BEST_SPEED); } catch (FileNotFoundException ex) {
ex.printStackTrace();
}private boolean compress(File file, String base) {
try {
if (file.isDirectory()) { File[] files = file.listFiles();
ZipEntry ze = new ZipEntry(base + "/");
zip.putNextEntry(ze);
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < files.length; i++) {
compress(files[i], base + files[i].getName());
} } else if (file.isFile()) {
if ("".equals(base)) {
zip.putNextEntry(new ZipEntry(base + "/"));
base = file.getName();
}
ZipEntry ze = new ZipEntry(base); zip.putNextEntry(ze); RandomAccessFile randomAccessFile = new RandomAccessFile(file.getPath(), "r"); final int MAX_SIZE = 1024 * 1024 * 2;
int size = (int) randomAccessFile.length();
int forsize = size / MAX_SIZE;
byte[] b;
if (forsize == 0) {
b = new byte[size];
randomAccessFile.read(b);
zip.write(b);
} else {
forsize += 1;
for (int i = 0; i < forsize; i++) {
b = new byte[MAX_SIZE];
int zzzz = randomAccessFile.read(b);
System.out.println(zzzz);
zip.write(b);
}
}
zip.closeEntry();
return true;
}
} catch (IOException ex) {
ex.printStackTrace();
}
return true;
}
使用了ant.jar包中的ZipOutputStream,但是压缩速度很慢。谢谢各位踊跃发言,呵呵代码如下:FileOutputStream fout = null;
BufferedOutputStream bos = null;
try {
fout = new FileOutputStream(path);
bos = new BufferedOutputStream(fout);
CheckedOutputStream csum = new CheckedOutputStream(bos, new Adler32());
zip = new ZipOutputStream(csum);
zip.setLevel(Deflater.BEST_SPEED); } catch (FileNotFoundException ex) {
ex.printStackTrace();
}private boolean compress(File file, String base) {
try {
if (file.isDirectory()) { File[] files = file.listFiles();
ZipEntry ze = new ZipEntry(base + "/");
zip.putNextEntry(ze);
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < files.length; i++) {
compress(files[i], base + files[i].getName());
} } else if (file.isFile()) {
if ("".equals(base)) {
zip.putNextEntry(new ZipEntry(base + "/"));
base = file.getName();
}
ZipEntry ze = new ZipEntry(base); zip.putNextEntry(ze); RandomAccessFile randomAccessFile = new RandomAccessFile(file.getPath(), "r"); final int MAX_SIZE = 1024 * 1024 * 2;
int size = (int) randomAccessFile.length();
int forsize = size / MAX_SIZE;
byte[] b;
if (forsize == 0) {
b = new byte[size];
randomAccessFile.read(b);
zip.write(b);
} else {
forsize += 1;
for (int i = 0; i < forsize; i++) {
b = new byte[MAX_SIZE];
int zzzz = randomAccessFile.read(b);
System.out.println(zzzz);
zip.write(b);
}
}
zip.closeEntry();
return true;
}
} catch (IOException ex) {
ex.printStackTrace();
}
return true;
}
if (f.isDirectory()) {
dirs++;
File[] fl = filter == null ? f.listFiles() : f.listFiles(filter);
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < fl.length; i++) {
zip(out, fl[i], base + fl[i].getName());
}
return;
}
out.putNextEntry(new ZipEntry(base));
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(f));
byte[] bys = new byte[1024];
while(in.read(bys) != -1) {
out.write(bys);
}
if(isDebug) {
System.out.printf("%8d %s add finished.%n", f.length(), f.getAbsolutePath());
}
} finally {
if(in != null) {
in.close();
}
}
items++;
len += f.length();
}
用了ant.jar包里的类就慢了。压入xml文件就不正确了。pdf文件是正确的
out.write(bys);
}应该是这样写出的问题。当文件读入一次没有1024字节时,后面都是空
看看这个http://blog.csdn.net/java2000_net/archive/2008/12/12/3502800.aspx
private void compress(File file, String base) {
FileInputStream fis = null;
BufferedInputStream in = null;
CheckedInputStream cis = null;
try {
if (file.isDirectory()) {
File[] files = file.listFiles();
ZipEntry ze = new ZipEntry(base + "/");
zipOS.putNextEntry(ze);
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < files.length; i++) {
compress(files[i], base + files[i].getName());
}
}
else {
if ("".equals(base)) {
// zip.putNextEntry(new ZipEntry(base + "/"));
base = file.getName();
} ZipEntry ze = new ZipEntry(base);
zipOS.putNextEntry(ze);
fis = new FileInputStream(file);
in = new BufferedInputStream(fis);
// cis = new CheckedInputStream(in, new Adler32());
byte[] bys;
int forsize = (int) (file.length() / MAX_SIZE_LONG);
if(forsize == 0) {
int size = (int) file.length();
bys = new byte[size];
in.read(bys);
zipOS.write(bys);
}
else {
for(int i=0; i<forsize; i++) {
bys = new byte[MAX_SIZE];
in.read(bys);
zipOS.write(bys);
}
int i = (int) (file.length() - MAX_SIZE_LONG * forsize);
if(i > 0) {
bys = new byte[i];
in.read(bys);
zipOS.write(bys);
}
}
zipOS.closeEntry();
} } catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if(cis != null)
cis.close();
if(in != null)
in.close();
if(fis != null)
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
} }解压代码:
private void deCompress(String toPath) {
try {
ZipEntry entry = zipIS.getNextEntry();
if(entry == null)
return;
String savePath = toPath + entry.getName(); File file = new File(savePath);
if(entry.isDirectory())
file.mkdir();
else {
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] data = new byte[MAX_SIZE];
int i = 0;
while((i = zipIS.read(data)) != -1) {
bos.write(data, 0, i);
}
bos.flush();
bos.close();
fos.close();
}
deCompress(toPath);
} catch (IOException ex) {
ex.printStackTrace();
}
}