转自:
Java API中的import java.util.zip.*;包下包含了Java对于压缩文件的所有相关操作。
我们可以使用该包中的方法,结合IO中的相关知识,进行文件的压缩和解压缩相关操作。
ZipFile
java中的每一个压缩文件都是可以使用ZipFile来进行表示的。
[java] view plaincopyprint?
-
File file = new File("F:/zippath.zip");
-
ZipFile zipFile = new ZipFile(file);
-
System.out.println("压缩文件的名称为:" + zipFile.getName());
压缩单个文件
[java] view plaincopyprint?
-
-
public static void ZipFile(String filepath ,String zippath) {
-
try {
-
File file = new File(filepath);
-
File zipFile = new File(zippath);
-
InputStream input = new FileInputStream(file);
-
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
-
zipOut.putNextEntry(new ZipEntry(file.getName()));
-
int temp = 0;
-
while((temp = input.read()) != -1){
-
zipOut.write(temp);
-
}
-
input.close();
-
zipOut.close();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
应用:
[java] view plaincopyprint?
-
ZipFile("d:/hello.txt", "d:/hello.zip");
压缩多个文件(文件夹)
[java] view plaincopyprint?
-
-
public static void ZipMultiFile(String filepath ,String zippath) {
-
try {
-
File file = new File(filepath);
-
File zipFile = new File(zippath);
-
InputStream input = null;
-
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
-
if(file.isDirectory()){
-
File[] files = file.listFiles();
-
for(int i = 0; i < files.length; ++i){
-
input = new FileInputStream(files[i]);
-
zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
-
int temp = 0;
-
while((temp = input.read()) != -1){
-
zipOut.write(temp);
-
}
-
input.close();
-
}
-
}
-
zipOut.close();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
应用:
[java] view plaincopyprint?
-
ZipMultiFile("f:/uu", "f:/zippath.zip");
解压缩单个文件
[java] view plaincopyprint?
-
-
public static void ZipContraFile(String zippath ,String outfilepath ,String filename) {
-
try {
-
File file = new File(zippath);
-
File outFile = new File(outfilepath);
-
ZipFile zipFile = new ZipFile(file);
-
ZipEntry entry = zipFile.getEntry(filename);
-
InputStream input = zipFile.getInputStream(entry);
-
OutputStream output = new FileOutputStream(outFile);
-
int temp = 0;
-
while((temp = input.read()) != -1){
-
output.write(temp);
-
}
-
input.close();
-
output.close();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
应用:
[java] view plaincopyprint?
-
ZipContraFile("d:/hello.zip","d:/eee.txt", "hello.txt");
解压缩多个文件
ZipInputStream类:
当我们需要解压缩多个文件的时候,ZipEntry就无法使用了。
如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类。
[java] view plaincopyprint?
-
-
-
-
-
-
public static void ZipContraMultiFile(String zippath ,String outzippath){
-
try {
-
File file = new File(zippath);
-
File outFile = null;
-
ZipFile zipFile = new ZipFile(file);
-
ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file));
-
ZipEntry entry = null;
-
InputStream input = null;
-
OutputStream output = null;
-
while((entry = zipInput.getNextEntry()) != null){
-
System.out.println("解压缩" + entry.getName() + "文件");
-
outFile = new File(outzippath + File.separator + entry.getName());
-
if(!outFile.getParentFile().exists()){
-
outFile.getParentFile().mkdir();
-
}
-
if(!outFile.exists()){
-
outFile.createNewFile();
-
}
-
input = zipFile.getInputStream(entry);
-
output = new FileOutputStream(outFile);
-
int temp = 0;
-
while((temp = input.read()) != -1){
-
output.write(temp);
-
}
-
input.close();
-
output.close();
-
}
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
}
应用:
[java] view plaincopyprint?
-
ZipContraMultiFile("f:/zippath.zip", "d:/");
-
ZipContraMultiFile("d:/hello.zip", "d:/");
阅读(262) | 评论(0) | 转发(0) |