java对多级目录文件夹压缩并解压(亲测不乱码)

本文介绍了如何使用Apache Ant库在Java中进行多级目录文件夹的压缩和解压操作,确保过程中不出现中文路径乱码问题。同时,还提到了文件夹的递归删除方法。
摘要由CSDN通过智能技术生成

文件夹压缩与解压:

利用apache提供的ant.jar,提供对单个文件与目录的压缩,并支持是否需要创建压缩源目录、中文路径并且提供了测试类


import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;

import java.io.*;
import java.util.Enumeration;
import java.util.zip.*;

/** 利用apache提供的ant.jar,提供对单个文件与目录的压缩,并支持是否需要创建压缩源目录、中文路径
 * @Title:
 * @Description:ZipCompress
 * @Version 1.2
 */
public class ZipCompress {
   

    private static boolean isCreateSrcDir = true;//是否创建源目录

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
   
        String src = "D:/组合件级/2";//指定压缩源,可以是目录或文件
        String decompressDir = "D:/test/最终目的";//解压路径
        String archive = "D:/组合件级/2.zip";//压缩包路径
        String comment = "无";//压缩包注释
        //----压缩文件或目录
        writeByApacheZipOutputStream(src,archive,comment);
        /*
         * 读压缩文件,注释掉,因为使用的是apache的压缩类,所以使用java类库中
         * 解压类时出错,这里不能运行
         */
       // readByZipInputStream(archive, decompressDir);
        //----使用apace ZipFile读取压缩文件
      //  readByApacheZipFile(archive, decompressDir);
    }
    /**对文件夹或者文件进行压缩
     *
     * @Time 2012-3-9 上午09:32:35 create
     * @param src
     * @param archive
     * @param comment
     * @throws FileNotFoundException
     * @throws IOException
     * @author jiangzhenming
     */
    public static void writeByApacheZipOutputStream(String src, String archive,
                                                    String comment) throws FileNotFoundException, IOException {
   
        //----压缩文件:
        FileOutputStream f = new FileOutputStream(archive);
        //使用指定校验和创建输出流
        CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32());

        ZipOutputStream zos = new 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是基于zziplib的支持压缩多级目录文件夹功能的代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <zzip/zzip.h> int main() { ZZIP_DIR *dir; zzip_error_t error; char *folder_name = "/path/to/folder"; // 指定要压缩文件夹路径 char *zipfile_name = "compressed.zip"; // 指定压缩后的文件名 dir = zzip_dir_open(folder_name, &error); if (!dir) { printf("Error opening directory: %s\n", zzip_strerror(error)); return 1; } ZZIP_DIRENT dirent; memset(&dirent, 0, sizeof(dirent)); zzip_dir_entry_init(&dirent); ZZIP_FILE *output_file = zzip_file_open(zipfile_name, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (!output_file) { printf("Error creating output file: %s\n", zzip_strerror_of(errno)); return 1; } zzip_dir_entry_set_name(&dirent, "/"); zzip_dir_entry_write(output_file, &dirent, 0); while (zzip_dir_read(dir, &dirent)) { char entry_path[1024]; sprintf(entry_path, "%s/%s", folder_name, dirent.d_name); ZZIP_FILE *input_file = zzip_file_open(entry_path, O_RDONLY); if (!input_file) { printf("Error opening file: %s\n", zzip_strerror_of(errno)); continue; } zzip_dir_entry_set_name(&dirent, dirent.d_name); zzip_dir_entry_write(output_file, &dirent, 0); char buffer[1024]; int read_bytes = zzip_file_read(input_file, buffer, sizeof(buffer)); while (read_bytes > 0) { zzip_file_write(output_file, buffer, read_bytes); read_bytes = zzip_file_read(input_file, buffer, sizeof(buffer)); } zzip_file_close(input_file); } zzip_file_close(output_file); zzip_dir_close(dir); printf("Successfully compressed folder '%s' to '%s'\n", folder_name, zipfile_name); return 0; } ``` 这个代码使用zziplib库打开指定的文件夹,并逐个读取其中的文件,将它们写入到压缩文件中。此外,还需要设置一个根目录的`/`目录项。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值