     let process = Process()
     process.executableURL = URL(fileURLWithPath: "/usr/bin/unzip", isDirectory: false)
     // Unzip the archive into a specified sub-folder and silence the output.
-    process.arguments = ["-q", sourceURL.path, "-d", destinationURL.path]
+    // "-j" is passed in to get rid of redundant subfolder.
+    process.arguments = ["-jq", sourceURL.path, "-d", destinationURL.path]
     
     try process.run()
     process.waitUntilExit()
 }
 
+/// Download file from portal and write the file(s) to appropriate path(s).
+///
+/// - Parameters:
+///   - sourceURL: The portal URL to the resource.
+///   - destinationURLProvider: A helper struct to make destination path URL with filename.
+///   - completion: A closure to handle the results.
 func downloadFile(at sourceURL: URL, destinationURLProvider: URLProvider, completion: @escaping (Result<URL, Error>) -> Void) {
     let downloadTask = URLSession.shared.downloadTask(with: sourceURL) { (temporaryURL, response, error) in
         if let temporaryURL = temporaryURL, let response = response {
             do {
                 let suggestedFilename = response.suggestedFilename!
-                let downloadURL: URL
-                var extractURL: URL?
+                let downloadName: String
                 let isArchive = (suggestedFilename as NSString).pathExtension == "zip"
-                downloadURL = destinationURLProvider.makeURL(filename: suggestedFilename)
+                // If the downloaded file is an archive and contains
+                //   - 1 file, use the name of that file.
+                //   - multiple files, use the suggested filename (*.zip).
+                // If it is not an archive, use the server suggested filename.
                 if isArchive {
-                    let fileCount = try numberOfFilesInArchive(at: temporaryURL)
-                    print("File count in the archive is \(fileCount)")
-                    // Extract to a sub-folder with the same name as the archive without the extension.
-                    extractURL = destinationURLProvider.makeSubFolderURLForArchive(folderName: (suggestedFilename as NSString).deletingPathExtension)
+                    let count = try numberOfFilesInArchive(at: temporaryURL)
+                    if count > 1 {
+                        downloadName = suggestedFilename
+                    } else {
+                        downloadName = try nameOfFileInArchive(at: temporaryURL)
+                    }
+                } else {
+                    downloadName = suggestedFilename
                 }
+                let downloadURL = destinationURLProvider.makeURL(filename: downloadName)
                 
-                try FileManager.default.createDirectory(at: downloadURL.deletingLastPathComponent(), withIntermediateDirectories: true)
+                try FileManager.default.createDirectory(
+                    at: downloadURL.deletingLastPathComponent(),
+                    withIntermediateDirectories: true
+                )
                 
                 if FileManager.default.fileExists(atPath: downloadURL.path) {
                     try FileManager.default.removeItem(at: downloadURL)
                 }
                 
                 if isArchive {
-                    try uncompressArchive(at: temporaryURL, to: extractURL!)
+                    let extractURL = downloadURL.pathExtension == "zip"
+                        // Uncompress to directory named after archive.
+                        ? downloadURL.deletingPathExtension()
+                        // Uncompress to appropriate subdirectory.
+                        : downloadURL.deletingLastPathComponent()
+                    try uncompressArchive(at: temporaryURL, to: extractURL)
                 } else {
                     try FileManager.default.moveItem(at: temporaryURL, to: downloadURL)
                 }


protocol URLProvider {
    func makeURL(filename: String) -> URL
    func makeSubFolderURLForArchive(folderName: String) -> URL
}

struct DestinationURLProvider: URLProvider {

        url.appendPathComponent(filename, isDirectory: false)
        return url
    }
}

/// Creates a URL such as `{portalURL}/sharing/rest/content/items/{itemIdentifier}/data`
