关于文件压缩与解压缩的那些事zipArchive

/**

     *  压缩文件的步骤,首先需要把zipArchive导入工程中,并添加 libz.dylib

     *

     * 创建压缩文件:

     * 1.制定一个路径

     * 2.利用方法 CreateZipFile2:创建压缩文件

     * 3.利用方法 CreateZipFile2: Password:创建带有密码的压缩文件

     * 4.给压缩文件中添加 文件

     * 4.关闭 CloseZipFile2(只有调用了这个方法,zip文件才会从内存中写入磁盘)

     *

     * 解压文件:

     * 1. 解压文件:UnzipOpenFile

     * 2. 把解压的文件存放到一个制定的目录:UnzipFileTo: overWrite

     * 3. 解压带有密码的文件

     *

     * 难点:

     * 文件路径错误,会导致意想不到的错误

     *

     * 代码说明:

     * 下面的代码主要的应用场景是,从网络上请求数据写入到压缩文件中,对这个压缩文件解压,得到一个密码和另外一个压缩文件,拿到密码后

     * 对得到的压缩文件进行解压。

     */

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *documentPath = [paths firstObject];

    

    

    

    NSString *filepath = [documentPath stringByAppendingPathComponent:@"wsy.zip"];

    NSString *encrypt = [documentPath stringByAppendingPathComponent:@"encrypt"];

    

    NSString *filepath2 = [documentPath stringByAppendingPathComponent:@"wsy/wsy.zip"];

    NSString *content = [documentPath stringByAppendingPathComponent:@"wsy/content"];

    NSString *image = [documentPath stringByAppendingPathComponent:@"wsy/image.jpg"];

    

    // documents下创建一个文件

    // ~~~~~~/documents/firmail

    NSString *firmailPath = [documentPath stringByAppendingPathComponent:@"wsy"];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    [fileManager createDirectoryAtPath:firmailPath withIntermediateDirectories:YES attributes:nil error:nil];

    

    ZipArchive *zip2 = [[ZipArchive alloc] init];

    // 创建压缩文件,带有密码

    BOOL ret3 = [zip2 CreateZipFile2:filepath2 Password:@"wsy"];

    // 路径:documents/firmail/firmail.zip

    // contentattachments/img.jpg加入到压缩文件中

    // firmail.zip中包含了contentttachments/img.jpg这两个文件

    

    // 给压缩文件中添加内容

    ret3 = [zip2 addFileToZip:content newname:@"content"];

    ret3 = [zip2 addFileToZip:image newname:@"attachments/img.jpg"];

    if (![zip2 CloseZipFile2]) {

        filepath2 = @"";

    }

    

    ZipArchive *archive = [[ZipArchive alloc] init];

    // 创建不带密码的压缩文件

    ret3 = [archive CreateZipFile2:filepath];

    // 路径:documents/firmail.zip

    // encrypt firmail.zip 加入到压缩文件中

    // firmail.zip中包含了contentfirmail.zip这两个文件

    

    ret3 = [archive addFileToZip:encrypt newname:@"encrypt"];

    ret3 = [archive addFileToZip:filepath2 newname:@"wsy.zip"];

    ret3 = [archive addFileToZip:[documentPath stringByAppendingPathComponent:@"image"] newname:@"header.jpg"];

    if (![archive CloseZipFile2]) {

        filepath2 = @"";

    }

    

    // 解压文件

    ZipArchive *unZip = [[ZipArchive alloc] init];

    // 把解压文件放入到这个路径下

     NSString *unZipPath = [documentPath stringByAppendingPathComponent:@"wsy"];

    ret3 = [unZip UnzipOpenFile:filepath];

    if (ret3) {

        // 把解压文件放入到一个路径下

        ret3 = [unZip UnzipFileTo:unZipPath overWrite:YES];

        if (ret3) {

            NSLog(@"解压成功");

            NSString *unzipPath2 = [unZipPath stringByAppendingPathComponent:@"wsy"];

            NSString *secretFirmailZip = [unZipPath stringByAppendingPathComponent:@"wsy.zip"];

            ZipArchive *unZipSecretFrimail = [[ZipArchive alloc] init];

            if ([unZipSecretFrimail UnzipOpenFile:secretFirmailZip]) {

                ret3 = [unZipSecretFrimail UnzipFileTo:unzipPath2 overWrite:YES];

                if (ret3) {

                    NSLog(@"二次解压成功");

                }

            }

        }else{

            NSLog(@"解压失败");

        }

        // 关闭解压

        [unZip UnzipCloseFile];

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java提供了ZipInputStream和ZipOutputStream类来实现对zip文件压缩压缩,同时也提供了压缩RAR文件的开源库,如junrar等。以下是一个示例代码,演示如何使用ZipInputStream和ZipEntry来压缩zip文件: ```java import java.io.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Unzip { public static void main(String[] args) throws IOException { String zipFilePath = "path/to/zip/file.zip"; String destDirectory = "path/to/destination/folder"; unzip(zipFilePath, destDirectory); } public static void unzip(String zipFilePath, String destDirectory) throws IOException { File destDir = new File(destDirectory); if (!destDir.exists()) { destDir.mkdir(); } ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath)); ZipEntry entry = zipIn.getNextEntry(); while (entry != null) { String filePath = destDirectory + File.separator + entry.getName(); if (!entry.isDirectory()) { extractFile(zipIn, filePath); } else { File dir = new File(filePath); dir.mkdir(); } zipIn.closeEntry(); entry = zipIn.getNextEntry(); } zipIn.close(); } private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath)); byte[] bytesIn = new byte[4096]; int read = 0; while ((read = zipIn.read(bytesIn)) != -1) { bos.write(bytesIn, 0, read); } bos.close(); } } ``` 对于RAR文件压缩,可以使用开源库junrar: ```java import com.github.junrar.Archive; import com.github.junrar.exception.RarException; import com.github.junrar.impl.FileVolumeManager; import com.github.junrar.rarfile.FileHeader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Unrar { public static void main(String[] args) throws RarException, IOException { String rarFilePath = "path/to/rar/file.rar"; String destDirectory = "path/to/destination/folder"; unrar(rarFilePath, destDirectory); } public static void unrar(String rarFilePath, String destDirectory) throws RarException, IOException { Archive archive = new Archive(new FileVolumeManager(new File(rarFilePath))); FileHeader fileHeader = archive.nextFileHeader(); while (fileHeader != null) { String filePath = destDirectory + File.separator + fileHeader.getFileNameString().trim(); if (fileHeader.isDirectory()) { File dir = new File(filePath); dir.mkdirs(); } else { FileOutputStream fos = new FileOutputStream(new File(filePath)); archive.extractFile(fileHeader, fos); fos.close(); } fileHeader = archive.nextFileHeader(); } archive.close(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值