java压缩解压缩.对于压缩包下,如果只是文件的话解压缩没有问题,如果压缩包下有文件夹就会出错.
比如c:\text.txt文件就能压缩,如果压缩包里有个文件夹,文件夹下是text.txt那么久报错了...
比如c:\text.txt文件就能压缩,如果压缩包里有个文件夹,文件夹下是text.txt那么久报错了
展开
1个回答
展开全部
给你一个例子看一下。
-----------------------------------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class Test {
public static void main(String[] args) throws Exception {
File file = new File("D:\\docs\\chm\\zip\\01.zip");
File outFile = new File("D:\\docs\\chm\\zip");
ZipFile zipFile = new ZipFile(file);
ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
ZipEntry entry = null;
InputStream input = null;
OutputStream out = null;
int len = 0;
byte[] buff = new byte[4096];
while ((entry = zipInput.getNextEntry()) != null) {
String name = entry.getName();
File dest = new File(outFile, name);
if (entry.isDirectory()) {
dest.mkdirs();
} else {
dest.getParentFile().mkdirs();
input = zipFile.getInputStream(entry);
out = new FileOutputStream(dest);
while ((len = input.read(buff)) != -1) {
out.write(buff, 0, len);
}
out.close();
input.close();
}
}
}
}
-----------------------------------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class Test {
public static void main(String[] args) throws Exception {
File file = new File("D:\\docs\\chm\\zip\\01.zip");
File outFile = new File("D:\\docs\\chm\\zip");
ZipFile zipFile = new ZipFile(file);
ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
ZipEntry entry = null;
InputStream input = null;
OutputStream out = null;
int len = 0;
byte[] buff = new byte[4096];
while ((entry = zipInput.getNextEntry()) != null) {
String name = entry.getName();
File dest = new File(outFile, name);
if (entry.isDirectory()) {
dest.mkdirs();
} else {
dest.getParentFile().mkdirs();
input = zipFile.getInputStream(entry);
out = new FileOutputStream(dest);
while ((len = input.read(buff)) != -1) {
out.write(buff, 0, len);
}
out.close();
input.close();
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询