标签:ext util ams exce port show hello 表示 put
打包单个文件:
public static void main(String[] args) throws IOException{
File file = new File("d:" + File.separator + "aaa.java");
File zipFile = new File("d:" + File.separator + "hello.zip");
InputStream input = new FileInputStream(file);
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(
zipFile));
zipOut.putNextEntry(new ZipEntry(file.getName()));
// 设置注释
zipOut.setComment("hello");
int temp = 0;
while((temp = input.read()) != -1){
zipOut.write(temp);
}
input.close();
zipOut.close();
}
打包多个文件:
public static void main(String[] args) throws IOException{
// 要被压缩的文件夹
File file = new File("d:" + File.separator + "temp");
File zipFile = new File("d:" + File.separator + "zipFile.zip");
InputStream input = null;
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(
zipFile));
zipOut.setComment("hello");
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();
}
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipFile;
/**
* ZipFile演示
* */
public class ZipFileDemo{
public static void main(String[] args) throws IOException{
File file = new File("d:" + File.separator + "hello.zip");
ZipFile zipFile = new ZipFile(file);
System.out.println("压缩文件的名称为:" + zipFile.getName());
}
}
public static void main(String[] args) throws IOException{
File file = new File("d:" + File.separator + "hello.zip");
File outFile = new File("d:" + File.separator + "unZipFile.txt");
ZipFile zipFile = new ZipFile(file);
ZipEntry entry = zipFile.getEntry("hello.txt");//hello.txt 为压缩包中文件的名称
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();
}
当我们需要解压缩多个文件的时候,ZipEntry就无法使用了,如果想操作更加复杂的压缩文件,我们就必须使用ZipInputStream类
public static void main(String[] args) throws IOException{
File file = new File("d:" + File.separator + "zipFile.zip");
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() + "文件");//entry.getName()获得压缩包内的文件及文件路径(aaaa/bb.txt)
outFile = new File("d:" + 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();
}
}
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow
标签:ext util ams exce port show hello 表示 put
原文地址:https://www.cnblogs.com/skinchqqhah/p/10350043.html