Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows Support #246

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,6 @@ Carthage/Build
.build/
.swiftpm/
Packages/

# vim
.*.sw?
26 changes: 22 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// swift-tools-version:5.1
// swift-tools-version:5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
import Foundation

let package = Package(
name: "Zip",
Expand All @@ -13,17 +15,33 @@ let package = Package(
dependencies: [],
path: "Zip/minizip",
exclude: ["module"],
linkerSettings: [
.linkedLibrary("z")
cSettings: [
.define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows])),
]),
.target(
name: "Zip",
dependencies: ["Minizip"],
path: "Zip",
exclude: ["minizip", "zlib"]),
exclude: ["minizip", "zlib"],
cSettings: [
.define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows])),
]),
.testTarget(
name: "ZipTests",
dependencies: ["Zip"],
path: "ZipTests"),
]
)

if let target = package.targets.filter({ $0.name == "Minizip" }).first {
#if os(Windows)
if ProcessInfo.processInfo.environment["ZIP_USE_DYNAMIC_ZLIB"] == nil {
target.cSettings?.append(contentsOf: [.define("ZLIB_STATIC")])
target.linkerSettings = [.linkedLibrary("zlibstatic")]
} else {
target.linkerSettings = [.linkedLibrary("zlib")]
}
#else
target.linkerSettings = [.linkedLibrary("z")]
#endif
}
4 changes: 2 additions & 2 deletions Zip/Zip.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,9 @@ public class Zip {
let fileNameSize = Int(fileInfo.size_filename) + 1
//let fileName = UnsafeMutablePointer<CChar>(allocatingCapacity: fileNameSize)
let fileName = UnsafeMutablePointer<CChar>.allocate(capacity: fileNameSize)
defer { fileName.deallocate() }

unzGetCurrentFileInfo64(zip, &fileInfo, fileName, UInt(fileNameSize), nil, 0, nil, 0)
unzGetCurrentFileInfo64(zip, &fileInfo, fileName, uLong(fileNameSize), nil, 0, nil, 0)
fileName[Int(fileInfo.size_filename)] = 0

var pathString = String(cString: fileName)
Expand All @@ -171,7 +172,6 @@ public class Zip {
if (fileName[fileInfoSizeFileName] == "/".cString(using: String.Encoding.utf8)?.first || fileName[fileInfoSizeFileName] == "\\".cString(using: String.Encoding.utf8)?.first) {
isDirectory = true;
}
free(fileName)
if pathString.rangeOfCharacter(from: CharacterSet(charactersIn: "/\\")) != nil {
pathString = pathString.replacingOccurrences(of: "\\", with: "/")
}
Expand Down
6 changes: 3 additions & 3 deletions Zip/minizip/include/Minizip.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#ifndef Minizip_h
#define Minizip_h

#import "crypt.h"
#import "unzip.h"
#import "zip.h"
#include "crypt.h"
#include "unzip.h"
#include "zip.h"

#endif /* Minizip_h */
1 change: 0 additions & 1 deletion Zip/minizip/module/module.modulemap
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module Minizip [system][extern_c] {
header "../include/Minizip.h"
link "z"
export *
}
8 changes: 8 additions & 0 deletions ZipTests/ZipTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ class ZipTests: XCTestCase {
let foundPermissions = try FileManager.default.attributesOfItem(atPath: permission644.path)[.posixPermissions] as? Int
#if os(Linux)
let expectedPermissions = 0o664
#elseif os(Windows)
let expectedPermissions = 0o700
#else
let expectedPermissions = 0o644
#endif
Expand All @@ -212,9 +214,15 @@ class ZipTests: XCTestCase {
let attributes777 = try fileManager.attributesOfItem(atPath: permission777.path)
let attributes600 = try fileManager.attributesOfItem(atPath: permission600.path)
let attributes604 = try fileManager.attributesOfItem(atPath: permission604.path)
#if os(Windows)
XCTAssertEqual(attributes777[.posixPermissions] as? Int, 0o700)
XCTAssertEqual(attributes600[.posixPermissions] as? Int, 0o700)
XCTAssertEqual(attributes604[.posixPermissions] as? Int, 0o700)
#else
XCTAssertEqual(attributes777[.posixPermissions] as? Int, 0o777)
XCTAssertEqual(attributes600[.posixPermissions] as? Int, 0o600)
XCTAssertEqual(attributes604[.posixPermissions] as? Int, 0o604)
#endif
}

func testQuickUnzipSubDir() throws {
Expand Down