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

用java程序在当前目录下创建一个子目录的方法是利用File对象的mkdirs方法。

完整代码如下:

// 获取当前图片的路径

String path = createImages.getAbsolutePath() + "/Images"

//创建文件对象f,根据path路径

File f = new File(path)

//如果当前不是一个目录就进入if

if (!f.isDirectory()) {

boolean success = f.mkdirs()//创建一个目录

if (success) { //成功打印当前的路径

System.out.println("Created path: " + f.getPath())

} else { //失败的情况

System.out.println("Could not create path: " + f.getPath())

}

} else {

System.out.println("Path exists: " + f.getPath())//子目录已存在。

}

关于mkdir:

mkdir()创建此抽象路径名称指定的目录(及只能创建一级的目录,且需要存在父目录),如果传入的path是多级路径,需要使用mkdirs()创建。


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

原文地址:https://54852.com/yw/11584742.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存