java zip压缩文件和目录



/**
 * 压缩文件
 *
 * @param 压缩源
 * @param 压缩zip
 * @param 压缩目标
 *
 * @throws IOException
 */
private static void zipFile(String source,
  ZipOutputStream zos, String target) throws IOException {
 // zip实体
 ZipEntry ze = new ZipEntry(target);
 zos.putNextEntry(ze);
 
 // 读取文件压缩
 FileInputStream fileStream = new FileInputStream(new File(source));
 int readLen = 0;
 byte[] buffer = new byte[8192];
 
 while((readLen = fileStream.read(buffer)) != -1){
  zos.write(buffer, 0, readLen);
 }
 
 fileStream.close();
}


/**
 * 压缩目录
 *
 * @param 压缩源
 * @param 压缩zip
 * @param 压缩目标
 *
 * @throws IOException
 */
private static void zipDir(String source,
  ZipOutputStream zos, String target) throws IOException {
 // zip实体
 ZipEntry ze = new ZipEntry(target);
 zos.putNextEntry(ze);
 
 // 目录下所有文件
 File dir = new File(source);
 android.util.Log.e(TAG,source);
 File[] fileList = dir.listFiles();
 
 if(fileList != null){
  //android.util.Log.e(TAG, "file szie " + fileList.length);
  for(File fileSub : fileList) {
   //android.util.Log.e(TAG, "file name " + fileSub.getName());
   //android.util.Log.e(TAG, "file path " + fileSub.getPath());
   if(fileSub.isDirectory()){
    zipDir(fileSub.getPath(), zos,  target + fileSub.getName() + "/");
   }else{
    zipFile(fileSub.getPath(), zos, target + fileSub.getName());
   }
  }
 }
}

/**
 * zip
 *
 * @param 压缩源
 * @param 压缩目标
 *
 * @throws IOException
 */
public static void zip(String source,
  String outfile) throws IOException {
 OutputStream os = null;
 ZipOutputStream zos = null;
 synchronized(class){
  os = new FileOutputStream(file, false);
  zos = new ZipOutputStream(new BufferedOutputStream(os));
  zipDir(source, zos, source.getName() + "/");
  zos.finish();
  zos.close();
 }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用ZipOutputStream类来实现zip压缩文件的功能,以下是一个简单的示例代码: ```java import java.io.*; import java.util.zip.*; public class ZipDemo { public static void main(String[] args) throws IOException { String sourceFile = "source.txt"; // 待压缩文件 String zipFile = "compressed.zip"; // 压缩后的文件名 FileOutputStream fos = new FileOutputStream(zipFile); ZipOutputStream zipOut = new ZipOutputStream(fos); File fileToZip = new File(sourceFile); FileInputStream fis = new FileInputStream(fileToZip); ZipEntry zipEntry = new ZipEntry(fileToZip.getName()); zipOut.putNextEntry(zipEntry); byte[] bytes = new byte[1024]; int length; while ((length = fis.read(bytes)) >= 0) { zipOut.write(bytes, 0, length); } zipOut.close(); fis.close(); fos.close(); } } ``` 该程序将会读取一个名为source.txt的文件,将其压缩成名为compressed.zipzip文件。程序中使用了Java的IO流和ZipOutputStream类来完成压缩操作,具体实现过程如下: 1. 创建一个ZipOutputStream对象,用于向压缩文件中写入数据。 2. 读取需要压缩的文件。 3. 创建一个ZipEntry对象,用于描述待压缩文件的名称和属性。 4. 将ZipEntry对象写入ZipOutputStream中。 5. 循环读取待压缩文件的内容,并将其写入ZipOutputStream中。 6. 关闭ZipOutputStream与输入流。 7. 压缩完成。 需要注意的是,Java中还有许多其他的压缩方式和库,例如GZIP、JAR等,可以根据需要选择适当的方式来完成压缩操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值