Skip to content

Commit

Permalink
Fix archive preview for big sur
Browse files Browse the repository at this point in the history
  • Loading branch information
chamburr committed Apr 10, 2021
1 parent d6452f3 commit 1643948
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 46 deletions.
26 changes: 20 additions & 6 deletions Glance.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -1220,11 +1220,20 @@

/* Begin XCRemoteSwiftPackageReference section */
7E5E49492455E3EB00C8C148 /* XCRemoteSwiftPackageReference "swift-exec" */ = {
10779BC22621502C008A903C /* XCRemoteSwiftPackageReference "ZIPFoundation" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/samuelmeuli/swift-exec";
repositoryURL = "https://github.com/weichsel/ZIPFoundation";
requirement = {
kind = upToNextMinorVersion;
minimumVersion = 0.1.1;
kind = upToNextMajorVersion;
minimumVersion = 0.9.12;
};
};
10779BE926217F25008A903C /* XCRemoteSwiftPackageReference "SWCompression" */ = {
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/tsolomko/SWCompression";
requirement = {
kind = upToNextMajorVersion;
minimumVersion = 4.5.8;
};
};
7E616BAE244997420043F7AB /* XCRemoteSwiftPackageReference "SwiftCSV" */ = {
Expand All @@ -1238,10 +1247,15 @@
/* End XCRemoteSwiftPackageReference section */

/* Begin XCSwiftPackageProductDependency section */
7E5E494A2455E3EB00C8C148 /* SwiftExec */ = {
10779BC32621502C008A903C /* ZIPFoundation */ = {
isa = XCSwiftPackageProductDependency;
package = 10779BC22621502C008A903C /* XCRemoteSwiftPackageReference "ZIPFoundation" */;
productName = ZIPFoundation;
};
10779BEA26217F25008A903C /* SWCompression */ = {
isa = XCSwiftPackageProductDependency;
package = 7E5E49492455E3EB00C8C148 /* XCRemoteSwiftPackageReference "swift-exec" */;
productName = SwiftExec;
package = 10779BE926217F25008A903C /* XCRemoteSwiftPackageReference "SWCompression" */;
productName = SWCompression;
};
7E616BAF244997420043F7AB /* SwiftCSV */ = {
isa = XCSwiftPackageProductDependency;
Expand Down
63 changes: 51 additions & 12 deletions Glance/Credits.rtf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{\rtf1\ansi\ansicpg1252\cocoartf2512
{\rtf1\ansi\ansicpg1252\cocoartf2578
\cocoatextscaling0\cocoaplatform0{\fonttbl\f0\fswiss\fcharset0 Helvetica-Bold;\f1\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;\red0\green0\blue0;}
{\*\expandedcolortbl;;\cssrgb\c0\c0\c0;}
Expand Down Expand Up @@ -96,6 +96,34 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\
\
\

\f0\b \cf0 \kerning1\expnd0\expndtw0 SWCompression
\f1\b0 \cf2 \expnd0\expndtw0\kerning0
\
\
MIT License\
\
Copyright (c) 2021 Timofey Solomko\
\
Permission is hereby granted, free of charge, to any person obtaining a copy\
of this software and associated documentation files (the "Software"), to deal\
in the Software without restriction, including without limitation the rights\
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\
copies of the Software, and to permit persons to whom the Software is\
furnished to do so, subject to the following conditions:\
\
The above copyright notice and this permission notice shall be included in all\
copies or substantial portions of the Software.\
\
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\
SOFTWARE.\
\
\
\pard\pardeftab720\partightenfactor0

\f0\b \cf2 SwiftCSV\
Expand All @@ -113,18 +141,29 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\
\
\
\pard\pardeftab720\partightenfactor0

\f0\b \cf2 SwiftExec\
\pard\pardeftab720\partightenfactor0

\f1\b0 \cf2 \
MIT License\
\f0\b \cf0 \kerning1\expnd0\expndtw0 ZIPFoundation
\f1\b0 \cf2 \expnd0\expndtw0\kerning0
\
Copyright (c) 2020 Samuel Meuli\
\
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\
\
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\
MIT License\
\
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.}
Copyright (c) 2017-2021 Thomas Zoechling (https://www.peakstep.com)\
\
Permission is hereby granted, free of charge, to any person obtaining a copy\
of this software and associated documentation files (the "Software"), to deal\
in the Software without restriction, including without limitation the rights\
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\
copies of the Software, and to permit persons to whom the Software is\
furnished to do so, subject to the following conditions:\
\
The above copyright notice and this permission notice shall be included in all\
copies or substantial portions of the Software.\
\
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\
SOFTWARE.}
70 changes: 56 additions & 14 deletions QLPlugin/Views/Previews/TARPreview.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Foundation
import os.log
import SwiftExec
// import Gzip
import SWCompression

/// View controller for previewing tarballs (may be gzipped).
class TARPreview: Preview {
Expand Down Expand Up @@ -30,22 +31,63 @@ class TARPreview: Preview {
}

private func runTARFilesCommand(filePath: String) throws -> String {
let result = try exec(
program: "/usr/bin/tar",
arguments: [
"--gzip", // Allows listing contents of `.tar.gz` files
"--list",
"--verbose",
"--file",
filePath,
]
)
return result.stdout ?? ""
let archiveURL = URL(fileURLWithPath: filePath)
let fileData = try Data(contentsOf: archiveURL, options: .mappedIfSafe)
let decompressedData = try? GzipArchive.unarchive(archive: fileData)
let archive = try TarContainer.info(container: decompressedData ?? fileData)

var entries = [String]()

for entry in archive {
var formatted = ""

if entry.type == .directory {
formatted += "drwxr-xr-x "
} else {
formatted += "-rw-r--r-- "
}

formatted += "0 user staff "
formatted += String(entry.size ?? 0) + " "

let modifiedYear = Calendar.current
.component(.year, from: entry.modificationTime ?? Date())
let currentYear = Calendar.current.component(.year, from: Date())

if modifiedYear == currentYear {
formatted += dateFormatter1.string(from: entry.modificationTime ?? Date()) + " "
} else {
formatted += dateFormatter2.string(from: entry.modificationTime ?? Date()) + " "
}

formatted += entry.name

entries.append(formatted)
}

return entries.joined(separator: "\n")
}

private func runGZIPSizeCommand(filePath: String) throws -> String {
let result = try exec(program: "/usr/bin/gzip", arguments: ["--list", filePath])
return result.stdout ?? ""
let archiveURL = URL(fileURLWithPath: filePath)
let fileData = try Data(contentsOf: archiveURL, options: .mappedIfSafe)
let decompressedData = try? GzipArchive.unarchive(archive: fileData)

let compressed = fileData.count
let uncompressed = decompressedData?.count ?? 0

var result =
" compressed uncompressed ratio uncompressed_name\n \(compressed) \(uncompressed) "

if uncompressed == 0 {
result += "0.0% "
} else {
result +=
String(format: "%.1f%% ", 100.0 - Double(compressed) / Double(uncompressed) * 100.0)
}

result += "archive.tar "
return result
}

/// Parses a date string from `tar` output to a `Date` object.
Expand Down
69 changes: 55 additions & 14 deletions QLPlugin/Views/Previews/ZIPPreview.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation
import os.log
import SwiftExec
import ZIPFoundation

class ZIPPreview: Preview {
let filesRegex =
Expand All @@ -15,21 +15,60 @@ class ZIPPreview: Preview {
}

private func runZIPInfoCommand(filePath: String) throws -> String {
do {
let result = try exec(
program: "/usr/bin/zipinfo",
arguments: [filePath]
)
return result.stdout ?? ""
} catch {
// Empty ZIP files are allowed, but return exit code 1
let error = error as! ExecError
let stdout = error.execResult.stdout ?? ""
if error.execResult.exitCode == 1, stdout.hasSuffix("Empty zipfile.") {
return stdout
let archiveURL = URL(fileURLWithPath: filePath)
guard let archive = Archive(url: archiveURL, accessMode: .read) else {
return ""
}

var result = ""
var entries = [String]()

var fileCount = 0
var uncompressed = 0
var compressed = 0

for entry in archive {
var formatted = ""

if entry.type == .directory {
formatted += "drwxr-xr-x "
} else {
formatted += "-rw-r--r-- "
}
throw error

formatted += "2.0 unx "
formatted += String(entry.uncompressedSize) + " "
formatted += "bx stor "
formatted += dateFormatter
.string(from: entry.fileAttributes[.modificationDate] as! Date) + " "
formatted += entry.path

entries.append(formatted)

fileCount += 1
uncompressed += entry.uncompressedSize
compressed += entry.compressedSize
}

result += "Archive: archive.zip\n"
result += "Zip file size: 0 bytes, number of entries: 0\n"
if entries.isEmpty {
result += "Empty zipfile."
return result
}

result += entries.joined(separator: "\n") + "\n"
result += String(fileCount) + " files, "
result += String(uncompressed) + " bytes uncompressed, "
result += String(compressed) + " bytes compressed: "
if uncompressed == 0 {
result += "0.0%"
} else {
result +=
String(format: "%.1f%%", 100.0 - Double(compressed) / Double(uncompressed) * 100.0)
}

return result
}

/// Parses the output of the `zipinfo` command.
Expand Down Expand Up @@ -83,9 +122,11 @@ class ZIPPreview: Preview {
}

func createPreviewVC(file: File) throws -> PreviewVC {
dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
let zipInfoOutput = try runZIPInfoCommand(filePath: file.path)

// Parse command output
dateFormatter.timeZone = TimeZone.current
let (fileTree, sizeUncompressed, compressionRatio) = parseZIPInfo(lines: zipInfoOutput)

// Build label
Expand Down

0 comments on commit 1643948

Please sign in to comment.