zip压缩文件2

时间:2021-12-08 11:51:22
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class zip2 {


/**
* @param args
*/
public static void main(String[] args) {
zip2 z=new zip2();
z.jieyamkdir();



}
public void jieyamkdir(){


try {
File file=new File("D:/"+File.separator+"zip");//要进行压缩的文件
File zipFile=new File("D:/"+File.separator+"zip.zip");//定义压缩后的文件
InputStream input=null;
ZipOutputStream zipOut=null;//压缩流对象
zipOut=new ZipOutputStream(new FileOutputStream(zipFile));


int tem=0;
if(file.isDirectory()){
int i=0;
File lists[]=file.listFiles();
for(;i<lists.length;++i)
{
input=new FileInputStream(lists[i]);
zipOut.putNextEntry(new ZipEntry(file.getName()+File.separator+lists[i].getName()));//设置实体对象
while((tem=input.read())!=-1)

{
zipOut.write(tem);
System.out.print(tem+" ");
}

input.close();
}
zipOut.setComment("hello");
zipOut.close();

  }



} catch (Exception e) {
// TODO: handle exception
}

}
public void jieya(){
try {
File file=new File("D:/"+File.separator+"zip");
File zipFile=new File("D:/"+File.separator+"zip.zio");

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
    }
public void yasu(){


try {
File file=new File("D:/"+File.separator+"zip.txt");//要进行压缩的文件
File zipFile=new File("D:/"+File.separator+"zip.zip");//定义压缩后的文件
InputStream input=new FileInputStream(file);
ZipOutputStream zipOut=null;//压缩流对象
zipOut=new ZipOutputStream(new FileOutputStream(zipFile));
zipOut.putNextEntry(new ZipEntry(file.getName()));//设置实体对象

int tem=0;
while((tem=input.read())!=-1)
{
zipOut.write(tem);
System.out.print(tem+" ");
}
zipOut.setComment("hello");
input.close();
zipOut.close();

} catch (Exception e) {
// TODO: handle exception
}

}
}