Skip to content

Commit 26a590d

Browse files
authored
s2: Add self-extracting archives (#329)
* s2: Add self-extracting archives Add `s2sx` that creates self-extracting archives for linux/windows/darwin AMD64 platforms.
1 parent 68cd769 commit 26a590d

13 files changed

+651
-21
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ _testmain.go
2222
*.exe
2323
*.test
2424
*.prof
25+
/s2/cmd/_sfx/sfx-exe

.goreleaser.yml

+27
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Make sure to check the documentation at http://goreleaser.com
33
before:
44
hooks:
5+
- ./gen.sh
6+
57
builds:
68
-
79
id: "s2c"
@@ -51,6 +53,30 @@ builds:
5153
- mips64le
5254
goarm:
5355
- 7
56+
-
57+
id: "s2sx"
58+
binary: s2sx
59+
main: ./s2/cmd/_s2sx/main.go
60+
env:
61+
- CGO_ENABLED=0
62+
goos:
63+
- aix
64+
- linux
65+
- freebsd
66+
- netbsd
67+
- windows
68+
- darwin
69+
goarch:
70+
- 386
71+
- amd64
72+
- arm
73+
- arm64
74+
- ppc64
75+
- ppc64le
76+
- mips64
77+
- mips64le
78+
goarm:
79+
- 7
5480

5581
archives:
5682
-
@@ -69,6 +95,7 @@ archives:
6995
- goos: windows
7096
format: zip
7197
files:
98+
- unpack/*
7299
- s2/LICENSE
73100
- s2/README.md
74101
checksum:

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ deploy:
5252
script: curl -sL https://git.io/goreleaser | VERSION=v0.157.0 bash || true
5353
on:
5454
tags: true
55-
condition: $TRAVIS_OS_NAME = linux
55+
condition: $TRAVIS_OS_NAME = linux AND $TRAVIS_CPU_ARCH = amd64
5656
go: 1.16.x
5757
branches:
5858
only:

gen.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
3+
cd s2/cmd/_s2sx/ || exit 1
4+
go generate .

s2/README.md

+89-20
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,20 @@ For big skips the decompressor is able to skip blocks without decompressing them
112112
## Single Blocks
113113

114114
Similar to Snappy S2 offers single block compression.
115-
Blocks do not offer the same flexibility and safety as streams, but may be preferable for very small payloads, less than 100K.
115+
Blocks do not offer the same flexibility and safety as streams,
116+
but may be preferable for very small payloads, less than 100K.
116117

117-
Using a simple `dst := s2.Encode(nil, src)` will compress `src` and return the compressed result. It is possible to provide a destination buffer. If the buffer has a capacity of `s2.MaxEncodedLen(len(src))` it will be used. If not a new will be allocated. Alternatively `EncodeBetter` can also be used for better, but slightly slower compression.
118+
Using a simple `dst := s2.Encode(nil, src)` will compress `src` and return the compressed result.
119+
It is possible to provide a destination buffer.
120+
If the buffer has a capacity of `s2.MaxEncodedLen(len(src))` it will be used.
121+
If not a new will be allocated.
118122

119-
Similarly to decompress a block you can use `dst, err := s2.Decode(nil, src)`. Again an optional destination buffer can be supplied.
120-
The `s2.DecodedLen(src)` can be used to get the minimum capacity needed. If that is not satisfied a new buffer will be allocated.
123+
Alternatively `EncodeBetter`/`EncodeBest` can also be used for better, but slightly slower compression.
124+
125+
Similarly to decompress a block you can use `dst, err := s2.Decode(nil, src)`.
126+
Again an optional destination buffer can be supplied.
127+
The `s2.DecodedLen(src)` can be used to get the minimum capacity needed.
128+
If that is not satisfied a new buffer will be allocated.
121129

122130
Block function always operate on a single goroutine since it should only be used for small payloads.
123131

@@ -151,23 +159,28 @@ Directories can be wildcards as well. testdir/*/*.txt will match testdir/subdir/
151159
152160
Options:
153161
-bench int
154-
Run benchmark n times. No output will be written
162+
Run benchmark n times. No output will be written
155163
-blocksize string
156-
Max block size. Examples: 64K, 256K, 1M, 4M. Must be power of two and <= 4MB (default "4M")
157-
-c Write all output to stdout. Multiple input files will be concatenated
164+
Max block size. Examples: 64K, 256K, 1M, 4M. Must be power of two and <= 4MB (default "4M")
165+
-c Write all output to stdout. Multiple input files will be concatenated
158166
-cpu int
159-
Compress using this amount of threads (default CPU_THREADS])
167+
Compress using this amount of threads (default 32)
160168
-faster
161-
Compress faster, but with a minor compression loss
169+
Compress faster, but with a minor compression loss
162170
-help
163-
Display help
171+
Display help
164172
-pad string
165-
Pad size to a multiple of this value, Examples: 500, 64K, 256K, 1M, 4M, etc (default "1")
166-
-q Don't write any output to terminal, except errors
173+
Pad size to a multiple of this value, Examples: 500, 64K, 256K, 1M, 4M, etc (default "1")
174+
-q Don't write any output to terminal, except errors
167175
-rm
168-
Delete source file(s) after successful compression
176+
Delete source file(s) after successful compression
169177
-safe
170-
Do not overwrite output files
178+
Do not overwrite output files
179+
-slower
180+
Compress more, but a lot slower
181+
-verify
182+
Verify written files
183+
171184
```
172185

173186
## s2d
@@ -184,17 +197,73 @@ Directories can be wildcards as well. testdir/*/*.txt will match testdir/subdir/
184197
185198
Options:
186199
-bench int
187-
Run benchmark n times. No output will be written
188-
-c Write all output to stdout. Multiple input files will be concatenated
200+
Run benchmark n times. No output will be written
201+
-c Write all output to stdout. Multiple input files will be concatenated
189202
-help
190-
Display help
191-
-q Don't write any output to terminal, except errors
203+
Display help
204+
-q Don't write any output to terminal, except errors
192205
-rm
193-
Delete source file(s) after successful decompression
206+
Delete source file(s) after successful decompression
194207
-safe
195-
Do not overwrite output files
208+
Do not overwrite output files
209+
-verify
210+
Verify files, but do not write output
211+
```
212+
213+
## s2sx: self-extracting archives
214+
215+
s2sx allows creating self-extracting archives with no dependencies.
216+
217+
By default, executables are created for the same platforms as the host os,
218+
but this can be overridden with `-os` parameter.
219+
220+
Extracted files have 0666 permissions, except when untar option used.
196221

197222
```
223+
Usage: s2sx [options] file1 file2
224+
225+
Compresses all files supplied as input separately.
226+
If files have '.s2' extension they are assumed to be compressed already.
227+
Output files are written as 'filename.s2sfx' and with '.exe' for windows targets.
228+
By default output files will be overwritten.
229+
230+
Wildcards are accepted: testdir/*.txt will compress all files in testdir ending with .txt
231+
Directories can be wildcards as well. testdir/*/*.txt will match testdir/subdir/b.txt
232+
233+
Options:
234+
-arch string
235+
Destination architecture (default "amd64")
236+
-c Write all output to stdout. Multiple input files will be concatenated
237+
-cpu int
238+
Compress using this amount of threads (default 32)
239+
-help
240+
Display help
241+
-os string
242+
Destination operating system (default "windows")
243+
-q Don't write any output to terminal, except errors
244+
-rm
245+
Delete source file(s) after successful compression
246+
-safe
247+
Do not overwrite output files
248+
-untar
249+
Untar on destination
250+
251+
Available platforms are:
252+
253+
* darwin-amd64
254+
* linux-amd64
255+
* windows-amd64
256+
```
257+
258+
### Self-extracting TAR files
259+
260+
If you wrap a TAR file you can specify `-untar` to make it untar on the destination host.
261+
262+
Files are extracted to the current folder with the path specified in the tar file.
263+
264+
Note that tar files are not validated before they are wrapped.
265+
266+
For security reasons files that move below the root folder are not allowed.
198267

199268
# Performance
200269

0 commit comments

Comments
 (0)