java 如何压缩文件夹_我应该如何在java中提取压缩文件夹?

我使用以下代码在

Java中提取zip文件.

import java.io.*;

import java.util.zip.*;

class testZipFiles

{

public static void main(String[] args)

{

try

{

String filename = "C:\\zip\\includes.zip";

testZipFiles list = new testZipFiles( );

list.getZipFiles(filename);

}

catch (Exception e)

{

e.printStackTrace();

}

}

public void getZipFiles(String filename)

{

try

{

String destinationname = "c:\\zip\\";

byte[] buf = new byte[1024];

ZipInputStream zipinputstream = null;

ZipEntry zipentry;

zipinputstream = new ZipInputStream(

new FileInputStream(filename));

zipentry = zipinputstream.getNextEntry();

while (zipentry != null)

{

//for each entry to be extracted

String entryName = zipentry.getName();

System.out.println("entryname "+entryName);

int n;

FileOutputStream fileoutputstream;

File newFile = new File(entryName);

String directory = newFile.getParent();

if(directory == null)

{

if(newFile.isDirectory())

break;

}

fileoutputstream = new FileOutputStream(

destinationname+entryName);

while ((n = zipinputstream.read(buf, 0, 1024)) > -1)

fileoutputstream.write(buf, 0, n);

fileoutputstream.close();

zipinputstream.closeEntry();

zipentry = zipinputstream.getNextEntry();

}//while

zipinputstream.close();

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

显然,由于break语句,这不会提取文件夹树.我试图使用递归来处理文件夹树但失败了.有人可以告诉我如何改进此代码来处理文件夹树而不是压缩的单级文件夹.

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值