Java程序实现压缩某目录

Java程序实现压缩某目录,第1张

public class TestZip {

public static String SERPEROT = /

public static int BUFFER =

public static void main(String args[]){

zip( e:/hello/ e:/hello zip )

}

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 = srcFile substring(srcFile lastIndexOf( / ) + srcFile length())

zip(zos file folder)

} catch (FileNotFoundException e) {

e printStackTrace()

}finally{

try{

if(zos != null){zos close()}

if(fos != null){fos close()}

}catch(Exception e){

e printStackTrace()

}

}

}

private static void zip(ZipOutputStream descFile File srcFile String srcfolder){

FileInputStream fis = null

System out println(srcFile isDirectory())

try{

if(srcFile isDirectory()){

File[] files = srcFile listFiles()

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

srcfolder = srcfolder length() == ? : srcfolder + /

System out println(srcfolder)

for(int i= i<files lengthi++){

zip(descFile files[i] srcfolder + files[i] getName())

}

}else{

descFile putNextEntry(new ZipEntry(srcfolder))

fis = new FileInputStream(srcFile)

byte[] bytes = new byte[ ]

int n =

while((n = fis read(bytes)) != ){

descFile write(bytes n)

}

}

}catch(Exception e){

e printStackTrace()

}finally{

try{

if(fis != null){fis close()}

}catch(Exception e){

e printStackTrace()

}

}

}

lishixinzhi/Article/program/Java/hx/201311/25760

用Eclipse可以将java文件打成.jar包,再用exe4打成exe文件,不过exe4貌似不是免费的,打成jar包的具体过程是File->Export->java->jar file->next->选择工程中的java文件->下面选择jar包的路径->next->main class礼选择主类->finish,就ok 了,注意,只有你的机子上安装了jre/jdk之后打出来的才是jar包,直接点击才能运行,不然打出来的是压缩包,也就是说jar包只有在装了java运行环境的机子上才可以运行


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12135818.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存