-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchive
More file actions
executable file
·49 lines (46 loc) · 1.62 KB
/
archive
File metadata and controls
executable file
·49 lines (46 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env bash
#*******************************************************************************
# Copyright (c) 2020 Eclipse Foundation and others.
# This program and the accompanying materials are made available
# under the terms of the Eclipse Public License 2.0
# which is available at http://www.eclipse.org/legal/epl-v20.html,
# or the MIT License which is available at https://opensource.org/licenses/MIT.
# SPDX-License-Identifier: EPL-2.0 OR MIT
#*******************************************************************************
BTWD="$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")"
# shellcheck disable=SC1090
. "${BTWD}/_bootstrap.shsource"
expand() {
local file="${1}"
local target="${2:-.}"
local strip="${3:-}"
mkdir -p "${target}"
if [[ $(file --mime-type -b "${file}") = "application/x-gzip" ]] || [[ $(file --mime-type -b "${file}") = "application/gzip" ]]; then
tar zxf "${file}" ${strip:+"--strip-components=1"} -C "${target}"
elif [[ $(file --mime-type -b "${file}") = "application/zip" ]]; then
if [[ -z "${strip:-}" ]]; then
unzip -q -d "${target}" "${file}"
else
_unzip_strip "${file}" "${target}"
fi
else
ERROR "unknown archive type %s" "$(realpath --relative-to="$(pwd)" "$(file --mime-type "${file}")")"
return 1
fi
}
_unzip_strip() {
local zip="${1}"
local dest="${2:-.}"
local temp
temp=$(mktemp -d)
unzip -q -d "${temp}" "${zip}"
mkdir -p "${dest}"
shopt -s dotglob
local f=("${temp}"/*)
if [[ ${#f[@]} -eq 1 ]] && [[ -d "${f[0]}" ]] ; then
mv "${temp}"/*/* "${dest}"
else
mv "${temp}"/* "${dest}"
fi
rmdir "${temp}"/* "${temp}"
}