I want to compress files and folders in the OS X program.

Asked 1 years ago, Updated 1 years ago, 77 views

I'm trying to create an OS X (Cocoa) application for file compression with Xcode 7.0+Swift2.
The library used for compression is ZipArchive (formerly SSZipArchive) and has two functions:

·Create compressed files from multiple files
createZipFileAtPath withFilesAtPaths

·Create a compressed file from a single folder and its contents
createZipFileAtPath withContentsOfDirectory

You can create compressed files with only one folder, but if you want to create compressed files based on multiple files and folders, you cannot mix folder paths with FilesAtPaths.

In that case, you can temporarily create a folder, copy everything you want to compress into it, and compress it with ContentsOfDirectory, but if it's a small file, it takes time to compress a large file into a temporary folder.

If possible, I would like to directly create a compressed file with a mixture of folders and files, but is there such a way?
Another library is fine.

[Additional note]
The development environment has changed to Swift 1.x → 2.x because the question has passed.

swift xcode macos

2022-09-30 20:53

2 Answers

Hi, I've never used that library, so I can't give you a good answer.

If that's the case, why don't you use the standard libz.dylib library instead of using that library to zip through multiple files and folders one by one in your program?

Only a part of it, but

 - (NSData*)zlibInflate
{
    if ([self length] == 0)
    {
        return self;
    }

    NSUInteger full_length = [self length];
    NSUInteger half_length = full_length/2;
    NSMutableData*decompressed = NSMutableDataDataWithLength: (full_length+half_length);
    int status;
    z_stream strm;

    strm.next_in=(Bytef*) [self bytes];
    strm.avail_in=(NMUInt) full_length;
    strm.total_out = 0;
    strm.zalloc = Z_NULL;
    strm.zfree=Z_NULL;

    if (inflateInit(&strm) == Z_OK)
    {
        BOOL done = FALSE;

        while(!done)
        {
            if(strm.total_out>=[decompressed length])
            {
                [decompressed increaseLengthBy: half_length];
            }

            strm.next_out = [decompressed mutableBytes] + strm.total_out;
            strm.avail_out=([decompressed length]-strm.total_out);

            status=inflate(&strm,Z_SYNC_FLUSH);

            if(status==Z_STREAM_END)
            {
                done = TRUE;
            }
            else if (status!=Z_OK)
            {
                break;
            }
        }

        if (inflateEnd(&strm) == Z_OK)
        {
            if(done)
            {
                [decompressed setLength: strm.total_out];

                return [NSDataDataWithData:decompressed];
            }
        }
    }

    return self;
}

For your information.


2022-09-30 20:53

If it takes time to copy, how about using symbolic link instead?


2022-09-30 20:53

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.