压缩的
importjavaioBufferedInputStream;
importjavaioBufferedOutputStream;
importjavaioFile;
importjavaioFileInputStream;
importjavaioFileOutputStream;
importjavautilzipZipEntry;
importjavautilzipZipOutputStream;
publicclassZip{
staticfinalintBUFFER=;
publicstaticvoidmain(Stringargv[]){
try{
BufferedInputStreamorigin=null;
FileOutputStreamdest=newFileOutputStream(E:\\test\\myfileszip);
ZipOutputStreamout=newZipOutputStream(newBufferedOutputStream(
dest));
bytedata[]=newbyte[BUFFER];
Filef=newFile(e:\\test\\a\\);
Filefiles[]=flistFiles();
for(inti=;i<fileslength;i++){
FileInputStreamfi=newFileInputStream(files[i]);
origin=newBufferedInputStream(fiBUFFER);
ZipEntryentry=newZipEntry(files[i]getName());
outputNextEntry(entry);
intcount;
while((count=originread(dataBUFFER))!=){
outwrite(datacount);
}
originclose();
}
outclose();
}catch(Exceptione){
eprintStackTrace();
}
}
}
解压缩的
importjavaioBufferedInputStream;
importjavaioBufferedOutputStream;
importjavaioFile;
importjavaioFileOutputStream;
importjavautilEnumeration;
importjavautilzipZipEntry;
importjavautilzipZipFile;
publicclassUnZip{
staticfinalintBUFFER=;
publicstaticvoidmain(Stringargv[]){
try{
StringfileName=E:\\test\\myfileszip;
StringfilePath=E:\\test\\;
ZipFilezipFile=newZipFile(fileName);
Enumerationemu=zipFileentries();
inti=;
while(emuhasMoreElements()){
ZipEntryentry=(ZipEntry)emunextElement();
//会把目录作为一个file读出一次所以只建立目录就可以之下的文件还会被迭代到
if(entryisDirectory())
{
newFile(filePath+entrygetName())mkdirs();
continue;
}
BufferedInputStreambis=newBufferedInputStream(zipFilegetInputStream(entry));
Filefile=newFile(filePath+entrygetName());
//加入这个的原因是zipfile读取文件是随机读取的这就造成可能先读取一个文件
//而这个文件所在的目录还没有出现过所以要建出目录来
Fileparent=filegetParentFile();
if(parent!=null&&(!parentexists())){
parentmkdirs();
}
FileOutputStreamfos=newFileOutputStream(file);
BufferedOutputStreambos=newBufferedOutputStream(fosBUFFER);
intcount;
bytedata[]=newbyte[BUFFER];
while((count=bisread(dataBUFFER))!=)
{
boswrite(datacount);
}
bosflush();
bosclose();
bisclose();
}
zipFileclose();
}catch(Exceptione){
eprintStackTrace();
}
}
}