Mac应用程序下载,验证(通过md5),并运行另一个大型应用程序?

时间:2016-05-17 20:33:24

标签: objective-c swift macos dmg bootcamp

我花了很长时间用谷歌搜索一个特定的产品解决方案,这在我的生活中似乎找不到,所以我来堆栈溢出寻求帮助。

我在一所未提及的公立大学工作,其中需要一组新入学的学生购买新的笔记本电脑,其中一个选项是13" Macbook Pro。我们预测,在8月秋季学期开始之前的几个月内,大约1,800名学生将会收购这些Macbook。

我们遇到的问题是他们需要使用一些仅在Windows中可用的特定软件。我们找到了一个产品解决方案,它运行一个简单的.dmg文件并对现有硬盘进行分区,以便在Boot Camp中安装预配置的Windows映像,其中包含所需的软件。通过这种方式,学生不需要执行任何超出技术知识范围的任务,因为Boot Camp有时会让人感到有些困惑。

我正在寻找的解决方案是执行以下操作的应用程序或方法;

  1. 下载可能很大的.dmg文件(超过40 GB)而学生不必输入任何输入,例如文件位于互联网上
  2. 通过md5校验和验证文件
  3. 如果可能,自动执行.dmg文件
  4. 或者这是否需要了解Swift / Objective-C才能通过自我发展来实现?

1 个答案:

答案 0 :(得分:3)

基本上,是的,你需要一些Swift / Cocoa框架语言才能这样做。您也可以使用其他语言(如Python)来执行此操作。

  1. 下载文件。您可以使用简单的下载程序类来完成此操作,如下所示:

    class HTTP {
        static func download(download_path: NSURL, save_path: NSURL, completion: ((NSError?, NSData?) -> Void)) {
            let req = NSMutableURLRequest(URL: download_path)
            req.HTTPMethod = "GET"
            NSURLSession.sharedSession().dataTaskWithRequest(req) { data, response, error in
                if error != nil {
                    //Downloading file failed
                    completion(error, nil)
                } else {
                    //Downloaded file
                    do {
                        //Write data to file:
                        if data != nil {
                            try data!.writeToFile(save_path.absoluteString + "file.dmg", options: [])
                            completion(nil, data!)
                        } else {
                            completion(NSError(domain: "Downloaded file null", code: -1, userInfo: nil), nil)
                        }
    
                    } catch {
                        completion(NSError(domain: "Could not write to file", code: -1, userInfo: nil), nil)
                    }
                }
            }.resume()
        }
    }
    
    //Usage: 
    HTTP.download(NSURL(string:"http://www.example.com/file.dmg")!, save_path:  NSURL(string: "/Users/username/Desktop/")!) {
        error, data in
    
        //Check if there's an error or data is null, if not continue to step 2
    }
    
  2. 检查MD5校验和。使用CommonCrypto Library计算文件数据的MD5:

    //Usage:
    "contents-of-file".MD5
    
  3. 要运行DMG,请查看running terminal commands from swifthow to run a DMG file from terminal。或者,您可以使用Applescript to run the DMGcall that script from swift

  4. 使用上述方法的完整示例:

    HTTP.download(NSURL(string:"http://www.example.com/file.dmg")!, save_path: NSURL(string: "/Users/username/Desktop/")!) {
        error, data in
        if error == nil {
            guard data != nil else {
                //Data is nil
            }
            if String(data: data!, encoding: NSUTF8StringEncoding)!.md5() == /* Other MD5 value to check */ {
                //MD5 checksum completed. Use whatever method here to execute the DMG
                executeDMG(NSURL(string: "/Users/username/Desktop/file.dmg")!)
            }
    
        } else {
            //Couldn't download file
            //Info: error.localizedDescription property
        }
    }
    
相关问题