问题描述
我Debug了查了很久··但是还是查不出所以然啦,可能是方法不对什么,请大侠帮帮忙importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStream;importjava.util.zip.ZipException;importjava.util.zip.ZipOutputStream;importjava.util.zip.ZipEntry;publicclassZipOutputStreamTest2{staticZipOutputStreamzip=null;staticInputStreamin=null;publicstaticvoidmain(String[]args)throwsException{FileZfile=newFile("D:"+File.separator+"javaTest"+File.separator+"ZipOutputStream2.zip");FileOfile=newFile("D:"+File.separator+"javaTest"+File.separator+"day");zip=newZipOutputStream(newZipOutputStream(newFileOutputStream(Zfile)));ZipEntryfirstEntry=newZipEntry(Ofile.getName());FileZip(Ofile,firstEntry);in.close();zip.close();}//如果文件夹里面还有文件夹,则要采用递归的方法压缩publicstaticvoidFileZip(FileInputFile,ZipEntryoldEntry)throwsException{FileInputFileCOPY=InputFile;ZipEntryoldEntryCOPY=oldEntry;inttemp=0;if(InputFileCOPY.isDirectory()){File[]list=InputFileCOPY.listFiles();for(inti=0;i<list.length;i++){if(list[i].isDirectory()){ZipEntrynewEntry=newZipEntry(oldEntryCOPY.getName()+File.separator+list[i].getName());FileZip(list[i],newEntry);}else{in=newFileInputStream(list[i]);ZipEntryze=newZipEntry(oldEntryCOPY.getName()+File.separator+list[i].getName());zip.putNextEntry(ze);while((temp=in.read())!=-1){zip.write(temp);}}}}}}
解决方案
解决方案二:
publicclassFieToZip{staticfinalintBUFFER=2048;staticbooleanflag=false;publicstaticFileZipSubdirectory(FilemyDir)throwsIOException{BufferedInputStreamorigin=null;FilezipFile=newFile("c://"+myDir.getName()+".zip");ZipOutputStreamout=newZipOutputStream(newFileOutputStream(zipFile));FiledirContents[]=myDir.listFiles();FiletempFile=null;try{for(inti=0;i<dirContents.length;i++){if(dirContents[i].isDirectory()){tempFile=ZipSubdirectory(dirContents[i]);flag=true;}else{tempFile=dirContents[i];flag=false;}FileInputStreamfis=newFileInputStream(tempFile);origin=newBufferedInputStream(fis,BUFFER);ZipEntryentry=neworg.apache.tools.zip.ZipEntry(tempFile.getName());bytedata[]=newbyte[BUFFER];intcount;out.putNextEntry(entry);while((count=origin.read(data,0,BUFFER))!=-1){out.write(data,0,count);}out.closeEntry();origin.close();if(flag==true){flag=tempFile.delete();}}}catch(Exceptione){System.out.println(e);}out.close();returnzipFile;}}
解决方案三:
???楼上是什么意思
解决方案四:
我怎么什么也不会,我看我是找不到工作了
解决方案五:
楼上是不是混分数的啊····
解决方案六:
压缩流没有用过
解决方案七:
丫的,这个错误真难找。zip=newZipOutputStream(newZipOutputStream(newFileOutputStream(Zfile)));
zip=newZipOutputStream(newFileOutputStream(Zfile));