遍历大量文件并分别压缩每个文件的最佳方法? [英] Best way to loop through a lot of files and zip each one separately?

查看:204
本文介绍了遍历大量文件并分别压缩每个文件的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要遍历相对较大的文件集(> 5000),分别将每个 (不是目录或文件组)分别压缩并上传到服务器.

I need to loop through a relatively large set of files (> 5000), zipping and uploading each one separately (not as directories or groups of files) to a server in turn.

我最大的问题是要知道哪种方法是实现便捷性和性能方面的最佳压缩方法.我认为必须有一个标准的Cocoa框架来处理似乎很普遍的要求,但似乎没有任何这样的框架.到目前为止,我发现了其他建议和方法:

The biggest part of my problem is to know which is the best way to do the zipping in terms of ease of implementation and performance. I thought there must be a standard Cocoa framework for something that is apparently a very common requirement but there doesn't seem to be any such framework. Other suggestions and approaches I've found so far:

  • 在code.google.com上 zip.frame ,它是可可简单的zip文件列表,读取和写入的框架.此框架的主要目的是通过提供本机Cocoa接口来防止您不得不在应用程序中使用命令行实用程序"-似乎很多人都找到了此链接(但我没注意到有人曾经真正使用过它!)
  • 同样在code.google.com上的
  • ziparchive -基于开放源代码'MiniZip '".
  • 关于使用NSTask调用命令行实用程序(例如 ditto )的建议很常见,例如在此
  • 某人的名为 ZipKit 的框架此处
  • 另一个CocoaDev问题讨论了几种方法,例如为C ++归档代码创建包装器,为 zlib minizip (minizip基于zlib构建)创建C包装器,等等
  • 关于 NSDataCategory 的东西(不理解)
  • 来自www.feedface.com的开源漫画/漫画书阅读器(!),名为 FFView ,它具有自己单独的
  • zip.framework at code.google.com, which "is a cocoa framework for easy zip file listing, reading and writing. The main purpose of this framework is to prevent you from having to use command line utilities in your application by providing a native Cocoa interface" - seems a lot of people have found this link (but I didn't notice anyone who had actually used it before!)
  • ziparchive also at code.google.com - "base on open source code 'MiniZip'".
  • Suggestions about using NSTask to call command line utilities such as ditto are common, such as in this CocoaDev question but I don't like the idea of having to do it
  • Similarly, someone here suggested NSTask to call zip and unzip - but this posting says that the "only problem is that when the files are decompressed, the Mac headers have been stripped, so Mac OS dosen't recognize the file!! (i.e. I zip an application and it stripps the "appl" from the file. When I unzip it, it is unusable."
  • Someone's framework called ZipKit here
  • Another CocoaDev question discusses several approaches e.g. creating wrapper for C++ archiving code, creating C wrapper for zlib and minizip (minizip is built around zlib), etc
  • Something about NSDataCategory (didn't understand it)
  • An open-source manga/comic book reader(!) from www.feedface.com called FFView which has its own separate archiving framework

压缩文件需要在Windows中解压缩.

The zipped files need to be unzipped in Windows.

请,我希望有人在解决方案方面具有实际经验,该解决方案可以满足与我类似的要求.如您所见,我已经找到了很多链接,因此仅是另一个链接到另一个框架/方法而没有实际表明它对我的问题的适用性并没有太大帮助.

Please, I hope someone has real-world experience with a solution that meets similar requirements to mine. As you can see, I've already found a lot of links so just another link to another framework/approach without something that actually indicates its applicability to my problem will not really be very helpful.

谢谢!

推荐答案

这就是我认为的适当的可可方式".您可能不喜欢它,但是它有效,它不需要外部框架,并且只需要很少的代码.

This is what I'd consider the "proper Cocoa way" to do it. You may not like it, but it works, it requires no external frameworks, and it requires very little code.

将Shell脚本添加到应用程序捆绑包zipmany.sh.

Add a shell script to your application bundle, zipmany.sh.

#!/bin/bash
set -e
SRC="$1"
DEST="$2"
cd "$SRC"
for FILE in $(find . -not -name '.*' -a type f)
do
    zip -jD "$DEST"/"$FILE".zip "$FILE"
done

然后在可可粉中

NSString *script = [[NSBundle mainBundle] pathForResource:@"zipmany" ofType:@"sh"];
NSTask *task = [[NSTask alloc] init];
[task setArguments:[NSArray arrayWithObjects:@"zipmany.sh", srcDir, destDir, nil]];
[task launch];
[task waitUntilExit];
if ([task terminationReason] == ATASK_SUCCESS_VALUE)
    succeeded;
else
    failed;

您可能会讨厌这种东西,但是将这种任务委派给一个单独的过程是健壮且相当标准的.

You may balk at this kind of stuff, but delegating this kind of task to a separate process is robust and fairly standard.

如果文件名可以包含空格,则必须对Shell脚本进行一些更改;我考虑过编写安全"版本,但这更具可读性.您还可以通过回显脚本的输出以供应用程序读取来制作进度条.

If the filenames can have spaces, you'll have to change the shell script a little bit; I considered writing the "safe" version but this is more readable. You can also make a progress bar by echoing output from the script to be read by the application.

waitUntilExit的调用将导致您的应用程序冻结或搁浅",除非您在单独的线程中运行整个程序,或者对Unix IPC足够了解以处理SIGCHLD.

The call to waitUntilExit will cause your app to freeze or "beach ball" unless you run the entire thing in a separate thread, or know enough about Unix IPC to handle SIGCHLD.

这篇关于遍历大量文件并分别压缩每个文件的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆