java

位置:IT落伍者 >> java >> 浏览文章

Java程序实现压缩某目录


发布日期:2020年01月27日
 
Java程序实现压缩某目录

public class TestZip {

public static String SERPEROT = /;

public static int BUFFER = ;

public static void main(String args[]){

zip(e:/hello/ e:/hellozip);

}

public static void zip(String srcFile String descFile){

ZipOutputStream zos = null;

FileOutputStream fos = null;

File file = null;

try {

fos = new FileOutputStream(descFile);

zos = new ZipOutputStream(fos);

file = new File(srcFile);

String folder = srcFilesubstring(srcFilelastIndexOf(/) +srcFilelength());

zip(zosfilefolder);

} catch (FileNotFoundException e) {

eprintStackTrace();

}finally{

try{

if(zos != null){zosclose();}

if(fos != null){fosclose();}

}catch(Exception e){

eprintStackTrace();

}

}

}

private static void zip(ZipOutputStream descFile File srcFileString srcfolder){

FileInputStream fis = null;

Systemoutprintln(srcFileisDirectory());

try{

if(srcFileisDirectory()){

File[] files = srcFilelistFiles();

descFileputNextEntry(new ZipEntry(srcfolder + /)); //是压缩包里面的路径

srcfolder = srcfolderlength() == ? : srcfolder + /;

Systemoutprintln(srcfolder);

for(int i= ; i<fileslength; i++){

zip(descFilefiles[i]srcfolder + files[i]getName());

}

}else{

descFileputNextEntry(new ZipEntry(srcfolder));

fis = new FileInputStream(srcFile);

byte[] bytes = new byte[];

int n =;

while((n = fisread(bytes)) != ){

descFilewrite(bytesn);

}

}

}catch(Exception e){

eprintStackTrace();

}finally{

try{

if(fis != null){fisclose();}

}catch(Exception e){

eprintStackTrace();

}

}

}

}

               

上一篇:用Java实现KeyFile

下一篇:Java实现多种幻灯片切换特效(附源码)