├── .github ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── ISSUE_TEMPLATE │ ├── bug.yml │ ├── config.yml │ ├── question.yml │ └── suggestion.yml ├── PULL_REQUEST_TEMPLATE.md ├── dependabot.yml ├── images │ ├── card.svg │ ├── godoc.svg │ └── license.svg └── workflows │ ├── ci.yml │ ├── codeql.yml │ └── godoc.yml ├── .typos.toml ├── LICENSE ├── Makefile ├── README.md ├── SECURITY.md ├── example_test.go ├── go.mod ├── go.sum ├── testdata ├── test-broken.7z ├── test-max-compression.7z ├── test-no-compression.7z ├── test.tar.bz2 ├── test.tar.gz ├── test.tar.xz ├── test.zip └── test │ ├── dir1 │ └── file1.log │ ├── file1.log │ ├── file2.log │ └── file3.log ├── zip7.go └── zip7_test.go /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Code of Conduct 2 | 3 | As contributors and maintainers of this project, and in the interest of 4 | fostering an open and welcoming community, we pledge to respect all people who 5 | contribute through reporting issues, posting feature requests, updating 6 | documentation, submitting pull requests or patches, and other activities. 7 | 8 | We are committed to making participation in this project a harassment-free 9 | experience for everyone, regardless of level of experience, gender, gender 10 | identity and expression, sexual orientation, disability, personal appearance, 11 | body size, race, ethnicity, age, religion, or nationality. 12 | 13 | Examples of unacceptable behavior by participants include: 14 | 15 | * The use of sexualized language or imagery 16 | * Personal attacks 17 | * Trolling or insulting/derogatory comments 18 | * Public or private harassment 19 | * Publishing other's private information, such as physical or electronic 20 | addresses, without explicit permission 21 | * Other unethical or unprofessional conduct 22 | 23 | Project maintainers have the right and responsibility to remove, edit, or 24 | reject comments, commits, code, wiki edits, issues, and other contributions 25 | that are not aligned to this Code of Conduct, or to ban temporarily or 26 | permanently any contributor for other behaviors that they deem inappropriate, 27 | threatening, offensive, or harmful. 28 | 29 | By adopting this Code of Conduct, project maintainers commit themselves to 30 | fairly and consistently applying these principles to every aspect of managing 31 | this project. Project maintainers who do not follow or enforce the Code of 32 | Conduct may be permanently removed from the project team. 33 | 34 | This Code of Conduct applies both within project spaces and in public spaces 35 | when an individual is representing the project or its community. 36 | 37 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 38 | reported by contacting a project maintainer at `conduct@essentialkaos.com`. All 39 | complaints will be reviewed and investigated and will result in a response that 40 | is deemed necessary and appropriate to the circumstances. Maintainers are 41 | obligated to maintain confidentiality with regard to the reporter of an 42 | incident. 43 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing Guidelines 2 | 3 | **IMPORTANT! Contribute your code only if you have an excellent understanding of project idea and all existing code base. Otherwise, a nicely formatted issue will be more helpful to us.** 4 | 5 | ### Issues 6 | 7 | 1. Provide product version where the problem was found; 8 | 2. Provide info about your environment; 9 | 3. Provide detailed info about your problem; 10 | 4. Provide steps to reproduce the problem; 11 | 5. Provide actual and expected results. 12 | 13 | ### Code 14 | 15 | 1. Check your code **before** creating pull request; 16 | 2. If tests are present in a project, add tests for your code; 17 | 3. Add inline documentation for your code; 18 | 4. Apply code style used throughout the project; 19 | 5. Create your pull request to `develop` branch (_pull requests to other branches are not allowed_). 20 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug.yml: -------------------------------------------------------------------------------- 1 | name: ❗ Bug Report 2 | description: File a bug report 3 | title: "[Bug]: " 4 | labels: ["issue • bug"] 5 | assignees: 6 | - andyone 7 | 8 | body: 9 | - type: markdown 10 | attributes: 11 | value: | 12 | > [!IMPORTANT] 13 | > Before you open an issue, search GitHub Issues for a similar bug reports. If so, please add a 👍 reaction to the existing issue. 14 | 15 | - type: textarea 16 | attributes: 17 | label: Module version info 18 | description: Output of `grep 'github.com/essentialkaos/zip7' go.sum` command 19 | render: shell 20 | validations: 21 | required: true 22 | 23 | - type: textarea 24 | attributes: 25 | label: Steps to reproduce 26 | description: Short guide on how to reproduce this problem on our site 27 | placeholder: | 28 | 1. [First Step] 29 | 2. [Second Step] 30 | 3. [and so on...] 31 | validations: 32 | required: true 33 | 34 | - type: textarea 35 | attributes: 36 | label: Expected behavior 37 | description: What you expected to happen 38 | validations: 39 | required: true 40 | 41 | - type: textarea 42 | attributes: 43 | label: Actual behavior 44 | description: What actually happened 45 | validations: 46 | required: true 47 | 48 | - type: textarea 49 | attributes: 50 | label: Additional info 51 | description: Include gist of relevant config, logs, etc. 52 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | 3 | contact_links: 4 | - name: Security Policies and Procedures 5 | url: https://github.com/essentialkaos/.github/blob/master/SECURITY.md 6 | about: Security procedures and general policies for all ESSENTIAL KAOS projects. 7 | 8 | - name: Contributing Guidelines 9 | url: https://github.com/essentialkaos/contributing-guidelines/blob/master/CONTRIBUTING.md 10 | about: Contributing Guidelines for all ESSENTIAL KAOS projects 11 | 12 | - name: Go Version Support Policy 13 | url: https://github.com/essentialkaos/.github/blob/master/GO-VERSION-SUPPORT.md 14 | about: Information about supported Go versions 15 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/question.yml: -------------------------------------------------------------------------------- 1 | name: ❓ Question 2 | description: Question about application, configuration or code 3 | title: "[Question]: " 4 | labels: ["issue • question"] 5 | assignees: 6 | - andyone 7 | 8 | body: 9 | - type: markdown 10 | attributes: 11 | value: | 12 | > [!IMPORTANT] 13 | > Before you open an issue, search GitHub Issues for a similar question. If so, please add a 👍 reaction to the existing issue. 14 | 15 | - type: textarea 16 | attributes: 17 | label: Question 18 | description: Detailed question 19 | validations: 20 | required: true 21 | 22 | - type: textarea 23 | attributes: 24 | label: Module version info 25 | description: Output of `grep 'github.com/essentialkaos/zip7' go.sum` command 26 | render: shell 27 | validations: 28 | required: true 29 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/suggestion.yml: -------------------------------------------------------------------------------- 1 | name: ➕ Suggestion 2 | description: Suggest new feature or improvement 3 | title: "[Suggestion]: " 4 | labels: ["issue • suggestion"] 5 | assignees: 6 | - andyone 7 | 8 | body: 9 | - type: markdown 10 | attributes: 11 | value: | 12 | > [!IMPORTANT] 13 | > Before you open an issue, search GitHub Issues for a similar feature requests. If so, please add a 👍 reaction to the existing issue. 14 | > 15 | > Opening a feature request kicks off a discussion. Requests may be closed if we're not actively planning to work on them. 16 | 17 | - type: textarea 18 | attributes: 19 | label: Proposal 20 | description: Description of the feature 21 | validations: 22 | required: true 23 | 24 | - type: textarea 25 | attributes: 26 | label: Current behavior 27 | description: What currently happens 28 | validations: 29 | required: true 30 | 31 | - type: textarea 32 | attributes: 33 | label: Desired behavior 34 | description: What you would like to happen 35 | validations: 36 | required: true 37 | 38 | - type: textarea 39 | attributes: 40 | label: Use case 41 | description: Why is this important (helps with prioritizing requests) 42 | validations: 43 | required: true 44 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | ### What did you implement: 2 | 3 | Closes #XXXXX 4 | 5 | ### How did you implement it: 6 | 7 | ... 8 | 9 | ### How can we verify it: 10 | 11 | ... 12 | 13 | ### TODO's: 14 | 15 | - [ ] Write tests 16 | - [ ] Write documentation 17 | - [ ] Check that there aren't other open pull requests for the same issue/feature 18 | - [ ] Format your source code by `make fmt` 19 | - [ ] Pass the test by `make test` 20 | - [ ] Provide verification config / commands 21 | - [ ] Enable "Allow edits from maintainers" for this PR 22 | - [ ] Update the messages below 23 | 24 | **Is this ready for review?:** No 25 | **Is it a breaking change?:** No 26 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | 3 | updates: 4 | - package-ecosystem: "gomod" 5 | directory: "/" 6 | target-branch: "develop" 7 | schedule: 8 | interval: "daily" 9 | timezone: "Etc/UTC" 10 | time: "03:00" 11 | labels: 12 | - "PR • MAINTENANCE" 13 | assignees: 14 | - "andyone" 15 | reviewers: 16 | - "andyone" 17 | groups: 18 | all: 19 | applies-to: version-updates 20 | update-types: 21 | - "minor" 22 | - "patch" 23 | 24 | - package-ecosystem: "github-actions" 25 | directory: "/" 26 | target-branch: "develop" 27 | schedule: 28 | interval: "daily" 29 | timezone: "Etc/UTC" 30 | time: "03:00" 31 | labels: 32 | - "PR • MAINTENANCE" 33 | assignees: 34 | - "andyone" 35 | reviewers: 36 | - "andyone" 37 | -------------------------------------------------------------------------------- /.github/images/card.svg: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.github/images/godoc.svg: -------------------------------------------------------------------------------- 1 | go: referencegoreference -------------------------------------------------------------------------------- /.github/images/license.svg: -------------------------------------------------------------------------------- 1 | license: Apache-2.0licenseApache-2.0 -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: [master, develop] 6 | pull_request: 7 | branches: [master] 8 | workflow_dispatch: 9 | inputs: 10 | force_run: 11 | description: 'Force workflow run' 12 | required: true 13 | type: choice 14 | options: [yes, no] 15 | 16 | permissions: 17 | actions: read 18 | contents: read 19 | statuses: write 20 | 21 | concurrency: 22 | group: ${{ github.workflow }}-${{ github.ref }} 23 | cancel-in-progress: true 24 | 25 | jobs: 26 | Go: 27 | name: Go 28 | runs-on: ubuntu-latest 29 | 30 | strategy: 31 | matrix: 32 | go: [ '1.22.x', '1.23.x' ] 33 | 34 | steps: 35 | - name: Install 7zip 36 | run: sudo apt-get install -y -qq p7zip-full 37 | 38 | - name: Print 7zip version 39 | run: 7za -h 40 | 41 | - name: Checkout 42 | uses: actions/checkout@v4 43 | 44 | - name: Set up Go 45 | uses: actions/setup-go@v5 46 | with: 47 | go-version: ${{ matrix.go }} 48 | 49 | - name: Download dependencies 50 | run: make deps 51 | 52 | - name: Run tests 53 | run: go test -covermode=count -coverprofile=cover.out 54 | 55 | - name: Send coverage data 56 | uses: essentialkaos/goveralls-action@v2 57 | env: 58 | COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }} 59 | with: 60 | profile: cover.out 61 | parallel: true 62 | flag-name: linux-${{ matrix.go }} 63 | 64 | SendCoverage: 65 | name: Send Coverage 66 | runs-on: ubuntu-latest 67 | 68 | needs: Go 69 | 70 | steps: 71 | - name: Finish parallel tests 72 | uses: essentialkaos/goveralls-action@v2 73 | env: 74 | COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }} 75 | with: 76 | parallel-finished: true 77 | 78 | Aligo: 79 | name: Aligo 80 | runs-on: ubuntu-latest 81 | 82 | needs: Go 83 | 84 | steps: 85 | - name: Checkout 86 | uses: actions/checkout@v4 87 | 88 | - name: Set up Go 89 | uses: actions/setup-go@v5 90 | with: 91 | go-version: '1.22.x' 92 | 93 | - name: Check Golang sources with Aligo 94 | uses: essentialkaos/aligo-action@v2 95 | with: 96 | files: ./... 97 | 98 | Typos: 99 | name: Typos 100 | runs-on: ubuntu-latest 101 | 102 | needs: Go 103 | 104 | steps: 105 | - name: Checkout 106 | uses: actions/checkout@v4 107 | 108 | - name: Check spelling 109 | continue-on-error: true 110 | uses: crate-ci/typos@master 111 | -------------------------------------------------------------------------------- /.github/workflows/codeql.yml: -------------------------------------------------------------------------------- 1 | name: "CodeQL" 2 | 3 | on: 4 | push: 5 | branches: [master, develop] 6 | pull_request: 7 | branches: [master] 8 | schedule: 9 | - cron: '0 3 * * */2' 10 | 11 | permissions: 12 | security-events: write 13 | actions: read 14 | contents: read 15 | 16 | jobs: 17 | analyse: 18 | name: Analyse 19 | runs-on: ubuntu-latest 20 | 21 | steps: 22 | - name: Checkout repository 23 | uses: actions/checkout@v4 24 | with: 25 | fetch-depth: 2 26 | 27 | - name: Initialize CodeQL 28 | uses: github/codeql-action/init@v3 29 | with: 30 | languages: go 31 | 32 | - name: Perform CodeQL Analysis 33 | uses: github/codeql-action/analyze@v3 34 | -------------------------------------------------------------------------------- /.github/workflows/godoc.yml: -------------------------------------------------------------------------------- 1 | name: GoDoc 2 | 3 | on: 4 | create 5 | 6 | jobs: 7 | GoDoc: 8 | name: Generate docs 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - name: Trigger GoSumDB and PkgGoDev 13 | uses: essentialkaos/godoc-action@v1 14 | -------------------------------------------------------------------------------- /.typos.toml: -------------------------------------------------------------------------------- 1 | [files] 2 | extend-exclude = ["go.sum"] 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2020 ESSENTIAL KAOS 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | 3 | # This Makefile generated by GoMakeGen 3.2.1 using next command: 4 | # gomakegen --mod . 5 | # 6 | # More info: https://kaos.sh/gomakegen 7 | 8 | ################################################################################ 9 | 10 | ifdef VERBOSE ## Print verbose information (Flag) 11 | VERBOSE_FLAG = -v 12 | endif 13 | 14 | ifdef PROXY ## Force proxy usage for downloading dependencies (Flag) 15 | export GOPROXY=https://proxy.golang.org/cached-only,direct 16 | endif 17 | 18 | ifdef CGO ## Enable CGO usage (Flag) 19 | export CGO_ENABLED=1 20 | else 21 | export CGO_ENABLED=0 22 | endif 23 | 24 | MAKEDIR = $(dir $(realpath $(firstword $(MAKEFILE_LIST)))) 25 | GITREV ?= $(shell test -s $(MAKEDIR)/.git && git rev-parse --short HEAD) 26 | 27 | ################################################################################ 28 | 29 | .DEFAULT_GOAL := help 30 | .PHONY = fmt vet deps update test init vendor mod-init mod-update mod-download mod-vendor help 31 | 32 | ################################################################################ 33 | 34 | init: mod-init ## Initialize new module 35 | 36 | deps: mod-download ## Download dependencies 37 | 38 | update: mod-update ## Update dependencies to the latest versions 39 | 40 | vendor: mod-vendor ## Make vendored copy of dependencies 41 | 42 | test: ## Run tests 43 | @echo "Starting tests…" 44 | ifdef COVERAGE_FILE ## Save coverage data into file (String) 45 | @go test $(VERBOSE_FLAG) -covermode=count -coverprofile=$(COVERAGE_FILE) ./. 46 | else 47 | @go test $(VERBOSE_FLAG) -covermode=count . 48 | endif 49 | 50 | mod-init: 51 | @echo "[1/2] Modules initialization…" 52 | ifdef MODULE_PATH ## Module path for initialization (String) 53 | @go mod init $(MODULE_PATH) 54 | else 55 | @go mod init 56 | endif 57 | 58 | @echo "[2/2] Dependencies cleanup…" 59 | ifdef COMPAT ## Compatible Go version (String) 60 | @go mod tidy $(VERBOSE_FLAG) -compat=$(COMPAT) -go=$(COMPAT) 61 | else 62 | @go mod tidy $(VERBOSE_FLAG) 63 | endif 64 | 65 | mod-update: 66 | @echo "[1/4] Updating dependencies…" 67 | ifdef UPDATE_ALL ## Update all dependencies (Flag) 68 | @go get -u $(VERBOSE_FLAG) all 69 | else 70 | @go get -u $(VERBOSE_FLAG) ./... 71 | endif 72 | 73 | @echo "[2/4] Stripping toolchain info…" 74 | @grep -q 'toolchain ' go.mod && go mod edit -toolchain=none || : 75 | 76 | @echo "[3/4] Dependencies cleanup…" 77 | ifdef COMPAT 78 | @go mod tidy $(VERBOSE_FLAG) -compat=$(COMPAT) 79 | else 80 | @go mod tidy $(VERBOSE_FLAG) 81 | endif 82 | 83 | @echo "[4/4] Updating vendored dependencies…" 84 | @test -d vendor && rm -rf vendor && go mod vendor $(VERBOSE_FLAG) || : 85 | 86 | mod-download: 87 | @echo "Downloading dependencies…" 88 | @go mod download 89 | 90 | mod-vendor: 91 | @echo "Vendoring dependencies…" 92 | @rm -rf vendor && go mod vendor $(VERBOSE_FLAG) || : 93 | 94 | fmt: ## Format source code with gofmt 95 | @echo "Formatting sources…" 96 | @find . -name "*.go" -exec gofmt -s -w {} \; 97 | 98 | vet: ## Runs 'go vet' over sources 99 | @echo "Running 'go vet' over sources…" 100 | @go vet -composites=false -printfuncs=LPrintf,TLPrintf,TPrintf,log.Debug,log.Info,log.Warn,log.Error,log.Critical,log.Print ./... 101 | 102 | help: ## Show this info 103 | @echo -e '\n\033[1mTargets:\033[0m\n' 104 | @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \ 105 | | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[33m%-6s\033[0m %s\n", $$1, $$2}' 106 | @echo -e '\n\033[1mVariables:\033[0m\n' 107 | @grep -E '^ifdef [A-Z_]+ .*?## .*$$' $(abspath $(lastword $(MAKEFILE_LIST))) \ 108 | | sed 's/ifdef //' \ 109 | | sort -h \ 110 | | awk 'BEGIN {FS = " .*?## "}; {printf " \033[32m%-13s\033[0m %s\n", $$1, $$2}' 111 | @echo -e '' 112 | @echo -e '\033[90mGenerated by GoMakeGen 3.2.1\033[0m\n' 113 | 114 | ################################################################################ 115 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | > [!CAUTION] 2 | > **This package is no longer maintained and should not be used. Use package [`bodgit/sevenzip`](https://github.com/bodgit/sevenzip) instead.** 3 | 4 |

5 | 6 |

7 | 8 | GoReportCard 9 | GitHub Actions CI Status 10 | GitHub Actions CodeQL Status 11 | 12 |

13 | 14 |

Compatibility and OS supportCI StatusContributingLicense

15 | 16 |
17 | 18 | `zip7` package provides methods for working with 7z archives (`p7zip` wrapper). 19 | 20 | ### Compatibility and OS support 21 | 22 | | Version | 1.x | 23 | |--------------|---------| 24 | | `p7zip 9.x` | Partial | 25 | | `p7zip 15.x` | Full | 26 | | `p7zip 16.x` | Full | 27 | 28 | | OS | Support | 29 | |----------|--------------------| 30 | | Linux | :heavy_check_mark: | 31 | | Mac OS X | :heavy_check_mark: | 32 | | FreeBSD | :heavy_check_mark: | 33 | | Windows | :x: | 34 | 35 | ### CI Status 36 | 37 | | Branch | Status | 38 | |--------|--------| 39 | | `master` | [![CI](https://kaos.sh/w/zip7/ci.svg?branch=master)](https://kaos.sh/w/zip7/ci?query=branch:master) | 40 | | `develop` | [![CI](https://kaos.sh/w/zip7/ci.svg?branch=develop)](https://kaos.sh/w/zip7/ci?query=branch:develop) | 41 | 42 | ### Contributing 43 | 44 | Before contributing to this project please read our [Contributing Guidelines](https://github.com/essentialkaos/contributing-guidelines#contributing-guidelines). 45 | 46 | ### License 47 | 48 | [Apache License, Version 2.0](https://www.apache.org/licenses/LICENSE-2.0) 49 | 50 |

51 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policies and Procedures 2 | 3 | This document outlines security procedures and general policies for all 4 | ESSENTIAL KAOS projects. 5 | 6 | * [Reporting a Bug](#reporting-a-bug) 7 | * [Disclosure Policy](#disclosure-policy) 8 | 9 | ## Reporting a Bug 10 | 11 | The ESSENTIAL KAOS team and community take all security bugs in our projects 12 | very seriously. Thank you for improving the security of our project. We 13 | appreciate your efforts and responsible disclosure and will make every effort 14 | to acknowledge your contributions. 15 | 16 | Report security bugs by emailing our security team at security@essentialkaos.com. 17 | 18 | The security team will acknowledge your email within 48 hours and will send a 19 | more detailed response within 48 hours, indicating the next steps in handling 20 | your report. After the initial reply to your report, the security team will 21 | endeavor to keep you informed of the progress towards a fix and full 22 | announcement, and may ask for additional information or guidance. 23 | 24 | Report security bugs in third-party dependencies to the person or team 25 | maintaining the dependencies. 26 | 27 | ## Disclosure Policy 28 | 29 | When the security team receives a security bug report, they will assign it to a 30 | primary handler. This person will coordinate the fix and release process, 31 | involving the following steps: 32 | 33 | * Confirm the problem and determine the affected versions; 34 | * Audit code to find any similar potential problems; 35 | * Prepare fixes for all releases still under maintenance. These fixes will be 36 | released as fast as possible. 37 | -------------------------------------------------------------------------------- /example_test.go: -------------------------------------------------------------------------------- 1 | package zip7 2 | 3 | // ////////////////////////////////////////////////////////////////////////////////// // 4 | // // 5 | // Copyright (c) 2024 ESSENTIAL KAOS // 6 | // Apache License, Version 2.0 // 7 | // // 8 | // ////////////////////////////////////////////////////////////////////////////////// // 9 | 10 | import ( 11 | "fmt" 12 | ) 13 | 14 | // ////////////////////////////////////////////////////////////////////////////////// // 15 | 16 | func ExampleAdd() { 17 | input := "log.txt" 18 | output := "log.7z" 19 | 20 | out, err := Add( 21 | Props{ 22 | File: output, 23 | Compression: 6, 24 | Password: "mYSuppaPAssWORD", 25 | Threads: 4, 26 | Delete: true, 27 | }, input) 28 | 29 | fmt.Printf("p7zip output: %s\n", out) 30 | fmt.Printf("Error: %v\n", err) 31 | } 32 | 33 | func ExampleAddList() { 34 | input1 := "log1.txt" 35 | input2 := "log2.txt" 36 | input3 := "log3.txt" 37 | output := "log.7z" 38 | 39 | out, err := AddList(Props{File: output}, []string{input1, input2, input3}) 40 | 41 | fmt.Printf("p7zip output: %s\n", out) 42 | fmt.Printf("error: %v\n", err) 43 | } 44 | 45 | func ExampleExtract() { 46 | out, err := Extract(Props{File: "log.7z"}) 47 | 48 | fmt.Printf("p7zip output: %s\n", out) 49 | fmt.Printf("Error: %v\n", err) 50 | } 51 | 52 | func ExampleList() { 53 | info, err := List(Props{File: "log.7z"}) 54 | 55 | if err != nil { 56 | fmt.Printf("Error: %v\n", err) 57 | return 58 | } 59 | 60 | fmt.Printf("Path: %s\n", info.Path) 61 | fmt.Printf("Type: %s\n", info.Type) 62 | fmt.Printf("Solid: %t\n", info.Solid) 63 | fmt.Printf("Blocks: %d\n", info.Blocks) 64 | fmt.Printf("PhysicalSize: %d\n", info.PhysicalSize) 65 | fmt.Printf("HeadersSize: %d\n", info.HeadersSize) 66 | 67 | fmt.Println("Files:") 68 | 69 | for _, file := range info.Files { 70 | fmt.Printf(" %s (size: %d)\n", file.Path, file.Size) 71 | } 72 | } 73 | 74 | func ExampleCheck() { 75 | ok, err := Check(Props{File: "log.7z"}) 76 | 77 | fmt.Printf("File ok: %t\n", ok) 78 | fmt.Printf("Error: %v\n", err) 79 | } 80 | 81 | func ExampleDelete() { 82 | out, err := Delete(Props{File: "log.7z"}, "dir1/file1", "dir2/file2") 83 | 84 | fmt.Printf("p7zip output: %s\n", out) 85 | fmt.Printf("Error: %v\n", err) 86 | } 87 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/essentialkaos/zip7 2 | 3 | go 1.22.8 4 | 5 | require ( 6 | github.com/essentialkaos/check v1.4.1 7 | github.com/essentialkaos/ek/v13 v13.15.3 8 | ) 9 | 10 | require ( 11 | github.com/kr/pretty v0.3.1 // indirect 12 | github.com/kr/text v0.2.0 // indirect 13 | github.com/rogpeppe/go-internal v1.13.1 // indirect 14 | golang.org/x/sys v0.28.0 // indirect 15 | ) 16 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= 2 | github.com/essentialkaos/check v1.4.1 h1:SuxXzrbokPGTPWxGRnzy0hXvtb44mtVrdNxgPa1s4c8= 3 | github.com/essentialkaos/check v1.4.1/go.mod h1:xQOYwFvnxfVZyt5Qvjoa1SxcRqu5VyP77pgALr3iu+M= 4 | github.com/essentialkaos/ek/v13 v13.15.3 h1:lDJ0qMs6wQRXWFuFRKnPvNX1SxvW2AowKgz6pRoMhAs= 5 | github.com/essentialkaos/ek/v13 v13.15.3/go.mod h1:ez9V1qvfXvjI6gqT24fZfkdVefHzYi6bm/c2NrD7B3s= 6 | github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= 7 | github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= 8 | github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= 9 | github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= 10 | github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= 11 | github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= 12 | github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR38lUII= 13 | github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o= 14 | golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA= 15 | golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 16 | -------------------------------------------------------------------------------- /testdata/test-broken.7z: -------------------------------------------------------------------------------- 1 | ABCD1234 2 | -------------------------------------------------------------------------------- /testdata/test-max-compression.7z: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/essentialkaos/zip7/619b87f2a555961b7554855cd091b6e94aa698a2/testdata/test-max-compression.7z -------------------------------------------------------------------------------- /testdata/test-no-compression.7z: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/essentialkaos/zip7/619b87f2a555961b7554855cd091b6e94aa698a2/testdata/test-no-compression.7z -------------------------------------------------------------------------------- /testdata/test.tar.bz2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/essentialkaos/zip7/619b87f2a555961b7554855cd091b6e94aa698a2/testdata/test.tar.bz2 -------------------------------------------------------------------------------- /testdata/test.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/essentialkaos/zip7/619b87f2a555961b7554855cd091b6e94aa698a2/testdata/test.tar.gz -------------------------------------------------------------------------------- /testdata/test.tar.xz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/essentialkaos/zip7/619b87f2a555961b7554855cd091b6e94aa698a2/testdata/test.tar.xz -------------------------------------------------------------------------------- /testdata/test.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/essentialkaos/zip7/619b87f2a555961b7554855cd091b6e94aa698a2/testdata/test.zip -------------------------------------------------------------------------------- /testdata/test/dir1/file1.log: -------------------------------------------------------------------------------- 1 | DIR1:FILE1 2 | -------------------------------------------------------------------------------- /testdata/test/file1.log: -------------------------------------------------------------------------------- 1 | FILE1 2 | -------------------------------------------------------------------------------- /testdata/test/file2.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/essentialkaos/zip7/619b87f2a555961b7554855cd091b6e94aa698a2/testdata/test/file2.log -------------------------------------------------------------------------------- /testdata/test/file3.log: -------------------------------------------------------------------------------- 1 | FILE3 -------------------------------------------------------------------------------- /zip7.go: -------------------------------------------------------------------------------- 1 | // Package zip7 provides methods for working with 7z archives (p7zip wrapper) 2 | package zip7 3 | 4 | // ////////////////////////////////////////////////////////////////////////////////// // 5 | // // 6 | // Copyright (c) 2024 ESSENTIAL KAOS // 7 | // Apache License, Version 2.0 // 8 | // // 9 | // ////////////////////////////////////////////////////////////////////////////////// // 10 | 11 | import ( 12 | "errors" 13 | "fmt" 14 | "os" 15 | "os/exec" 16 | "strconv" 17 | "strings" 18 | "time" 19 | ) 20 | 21 | // ////////////////////////////////////////////////////////////////////////////////// // 22 | 23 | // List of supported formats 24 | const ( 25 | TYPE_7Z = "7z" 26 | TYPE_ZIP = "zip" 27 | TYPE_GZIP = "gzip" 28 | TYPE_XZ = "xz" 29 | TYPE_BZIP = "bzip2" 30 | ) 31 | 32 | // ////////////////////////////////////////////////////////////////////////////////// // 33 | 34 | const _BINARY = "7za" 35 | 36 | const ( 37 | _COMPRESSION_MIN = 0 38 | _COMPRESSION_MAX = 9 39 | _COMPRESSION_DEFAULT = 4 40 | ) 41 | 42 | const ( 43 | _COMMAND_ADD = "a" 44 | _COMMAND_BENCHMARK = "b" 45 | _COMMAND_DELETE = "d" 46 | _COMMAND_LIST = "l" 47 | _COMMAND_TEST = "t" 48 | _COMMAND_UPDATE = "u" 49 | _COMMAND_EXTRACT = "x" 50 | ) 51 | 52 | const _TEST_OK_VALUE = "Everything is Ok" 53 | const _TEST_ERROR_VALUE = "ERRORS:" 54 | 55 | // ////////////////////////////////////////////////////////////////////////////////// // 56 | 57 | // Props contains properties for packing/unpacking data 58 | type Props struct { 59 | Dir string // Directory with files (for relative paths) 60 | File string // Output file name 61 | IncludeFile string // File with include filenames 62 | Exclude string // Exclude filenames 63 | ExcludeFile string // File with exclude filenames 64 | OutputDir string // Output dir (for extract command) 65 | Password string // Password 66 | WorkingDir string // Working dir 67 | Compression int // Compression level (0-9) 68 | Threads int // Number of CPU threads 69 | Recursive bool // Recurse subdirectories 70 | Delete bool // Delete files after compression 71 | } 72 | 73 | // Info contains info about archive 74 | type Info struct { 75 | Method []string 76 | Files []*FileInfo 77 | Path string 78 | Type string 79 | Blocks int 80 | PhysicalSize int 81 | HeadersSize int 82 | Solid bool 83 | } 84 | 85 | // FileInfo contains info about file inside archive 86 | type FileInfo struct { 87 | Modified time.Time 88 | Created time.Time 89 | Accessed time.Time 90 | Method []string 91 | Path string 92 | Folder string 93 | Attributes string 94 | Comment string 95 | HostOS string 96 | Size int 97 | PackedSize int 98 | CRC int 99 | Block int 100 | Version int 101 | Encrypted bool 102 | } 103 | 104 | // ////////////////////////////////////////////////////////////////////////////////// // 105 | 106 | // Add adds file or files to archive 107 | func Add(props Props, files ...string) (string, error) { 108 | return AddList(props, files) 109 | } 110 | 111 | // AddList adds files to archive from slice 112 | func AddList(props Props, files []string) (string, error) { 113 | if len(files) == 0 { 114 | return "", errors.New("You should define files to compress") 115 | } 116 | 117 | var cwd string 118 | 119 | err := props.Validate(false) 120 | 121 | if err != nil { 122 | return "", err 123 | } 124 | 125 | if props.Dir != "" { 126 | cwd, err = os.Getwd() 127 | 128 | if err != nil { 129 | return "", err 130 | } 131 | 132 | err = os.Chdir(props.Dir) 133 | 134 | if err != nil { 135 | return "", err 136 | } 137 | } 138 | 139 | out, err := execBinary(_COMMAND_ADD, props, files) 140 | 141 | if err != nil { 142 | return "", err 143 | } 144 | 145 | if props.Dir != "" { 146 | err = os.Chdir(cwd) 147 | 148 | if err != nil { 149 | return "", err 150 | } 151 | } 152 | 153 | return out, err 154 | } 155 | 156 | // Extract extracts archive 157 | func Extract(props Props) (string, error) { 158 | err := props.Validate(true) 159 | 160 | if err != nil { 161 | return "", err 162 | } 163 | 164 | return execBinary(_COMMAND_EXTRACT, props, nil) 165 | } 166 | 167 | // List returns info about archive 168 | func List(props Props) (*Info, error) { 169 | err := props.Validate(true) 170 | 171 | if err != nil { 172 | return nil, err 173 | } 174 | 175 | out, err := execBinary(_COMMAND_LIST, props, nil) 176 | 177 | if err != nil { 178 | return nil, err 179 | } 180 | 181 | return parseInfoString(out), nil 182 | } 183 | 184 | // Check tests archive 185 | func Check(props Props) (bool, error) { 186 | err := props.Validate(true) 187 | 188 | if err != nil { 189 | return false, err 190 | } 191 | 192 | out, err := execBinary(_COMMAND_TEST, props, nil) 193 | 194 | if err != nil { 195 | return false, err 196 | } 197 | 198 | outData := strings.Split(out, "\n") 199 | 200 | for index, line := range outData { 201 | if line == _TEST_OK_VALUE { 202 | return true, nil 203 | } else if line == _TEST_ERROR_VALUE { 204 | return false, errors.New(outData[index+1]) 205 | } 206 | } 207 | 208 | return false, errors.New("Can't parse 7zip output") 209 | } 210 | 211 | // Delete removes files from archive 212 | func Delete(props Props, files ...string) (string, error) { 213 | if len(files) == 0 { 214 | return "", errors.New("You should define files to delete") 215 | } 216 | 217 | err := props.Validate(true) 218 | 219 | if err != nil { 220 | return "", err 221 | } 222 | 223 | return execBinary(_COMMAND_DELETE, props, files) 224 | } 225 | 226 | // ////////////////////////////////////////////////////////////////////////////////// // 227 | 228 | // Validate validates properties values 229 | func (p Props) Validate(checkFile bool) error { 230 | switch { 231 | case checkFile && !isExist(p.File): 232 | return fmt.Errorf("File %s does not exist", p.File) 233 | 234 | case p.IncludeFile != "" && !isExist(p.IncludeFile): 235 | return fmt.Errorf("Included file %s does not exist", p.IncludeFile) 236 | 237 | case p.ExcludeFile != "" && !isExist(p.ExcludeFile): 238 | return fmt.Errorf("Included file %s does not exist", p.ExcludeFile) 239 | 240 | case p.OutputDir != "" && !isExist(p.OutputDir): 241 | return fmt.Errorf("Directory %s does not exist", p.OutputDir) 242 | } 243 | 244 | return nil 245 | } 246 | 247 | // ToArgs converts properties to p7zip arguments 248 | func (p Props) ToArgs(command string) []string { 249 | var args = []string{p.File, "", "-y", "-bd"} 250 | 251 | switch command { 252 | case _COMMAND_ADD: 253 | var compression int 254 | 255 | if p.Compression == 0 { 256 | compression = _COMPRESSION_DEFAULT 257 | } else { 258 | compression = between(p.Compression, _COMPRESSION_MIN, _COMPRESSION_MAX) 259 | } 260 | 261 | args = append(args, "-mx="+strconv.Itoa(compression)) 262 | 263 | switch { 264 | case p.Threads < 1: 265 | args = append(args, "-mmt=1") 266 | case p.Threads >= 1: 267 | args = append(args, "-mmt="+strconv.Itoa(between(p.Threads, 1, 128))) 268 | } 269 | 270 | if p.Exclude != "" { 271 | args = append(args, "-x"+p.Exclude) 272 | } else if p.ExcludeFile != "" { 273 | args = append(args, "-xr@"+p.ExcludeFile) 274 | } 275 | 276 | if p.IncludeFile != "" { 277 | args = append(args, "-ir@"+p.IncludeFile) 278 | } 279 | case _COMMAND_EXTRACT: 280 | if p.OutputDir != "" { 281 | args = append(args, "-o"+p.OutputDir) 282 | } 283 | case _COMMAND_LIST: 284 | args = append(args, "-slt") 285 | } 286 | 287 | if p.Password != "" { 288 | args = append(args, "-p"+p.Password) 289 | } 290 | 291 | if p.Recursive { 292 | args = append(args, "-r") 293 | } 294 | 295 | if p.WorkingDir != "" { 296 | args = append(args, "-w"+p.WorkingDir) 297 | } 298 | 299 | return args 300 | } 301 | 302 | // ////////////////////////////////////////////////////////////////////////////////// // 303 | 304 | // execBinary execs 7zip binary 305 | func execBinary(command string, props Props, files []string) (string, error) { 306 | args := props.ToArgs(command) 307 | 308 | if len(files) != 0 { 309 | args = append(args, files...) 310 | } 311 | 312 | cmd := exec.Command(_BINARY) 313 | 314 | cmd.Args = append(cmd.Args, command) 315 | cmd.Args = append(cmd.Args, args...) 316 | 317 | out, err := cmd.Output() 318 | 319 | if err != nil { 320 | return string(out), errors.New(string(out)) 321 | } 322 | 323 | return string(out), nil 324 | } 325 | 326 | // parseInfoString process raw info data 327 | func parseInfoString(infoData string) *Info { 328 | var data = strings.Split(infoData, "\n") 329 | var info = &Info{} 330 | 331 | header, headerEnd := extractInfoHeader(data) 332 | headerData := parseRecordData(header) 333 | 334 | info.Path = headerData["Path"] 335 | info.Type = headerData["Type"] 336 | info.Method = strings.Split(headerData["Method"], " ") 337 | 338 | if info.Type == TYPE_7Z { 339 | info.Solid = headerData["Solid"] == "+" 340 | 341 | info.Blocks, _ = strconv.Atoi(headerData["Blocks"]) 342 | info.PhysicalSize, _ = strconv.Atoi(headerData["Physical Size"]) 343 | info.HeadersSize, _ = strconv.Atoi(headerData["Headers Size"]) 344 | } 345 | 346 | recStart := 0 347 | records := data[headerEnd : len(data)-1] 348 | 349 | for i, v := range records { 350 | if v == "" { 351 | info.Files = append(info.Files, parseFileInfo(records[recStart:i])) 352 | recStart = i + 1 353 | } 354 | } 355 | 356 | return info 357 | } 358 | 359 | // parseFileInfo process raw info about file/directory 360 | func parseFileInfo(data []string) *FileInfo { 361 | var info = &FileInfo{} 362 | var recordData = parseRecordData(data) 363 | 364 | crc, _ := strconv.ParseInt(recordData["CRC"], 16, 0) 365 | 366 | info.Path = recordData["Path"] 367 | info.Folder = recordData["Folder"] 368 | info.Size, _ = strconv.Atoi(recordData["Size"]) 369 | info.PackedSize, _ = strconv.Atoi(recordData["Packed Size"]) 370 | info.Modified = parseDateString(recordData["Modified"]) 371 | info.Created = parseDateString(recordData["Created"]) 372 | info.Accessed = parseDateString(recordData["Accessed"]) 373 | info.Attributes = recordData["Attributes"] 374 | info.CRC = int(crc) 375 | info.Comment = recordData["Comment"] 376 | info.Encrypted = recordData["Encrypted"] == "+" 377 | info.Method = strings.Split(recordData["Method"], " ") 378 | info.Block, _ = strconv.Atoi(recordData["Block"]) 379 | info.HostOS = recordData["Host OS"] 380 | info.Version, _ = strconv.Atoi(recordData["Version"]) 381 | 382 | return info 383 | } 384 | 385 | // parseRecordData parse raw record 386 | func parseRecordData(data []string) map[string]string { 387 | var result = make(map[string]string) 388 | 389 | for _, rec := range data { 390 | if rec != "" { 391 | name, val := parseValue(rec) 392 | result[name] = val 393 | } 394 | } 395 | 396 | return result 397 | } 398 | 399 | // parseDateString parse date string 400 | func parseDateString(data string) time.Time { 401 | if data == "" { 402 | return time.Time{} 403 | } 404 | 405 | year, _ := strconv.Atoi(data[0:4]) 406 | month, _ := strconv.Atoi(data[5:7]) 407 | day, _ := strconv.Atoi(data[8:10]) 408 | hour, _ := strconv.Atoi(data[11:13]) 409 | min, _ := strconv.Atoi(data[14:16]) 410 | sec, _ := strconv.Atoi(data[17:19]) 411 | 412 | return time.Date(year, time.Month(month), day, hour, min, sec, 0, time.UTC) 413 | } 414 | 415 | // extractInfoHeader extracts header from raw info data 416 | func extractInfoHeader(data []string) ([]string, int) { 417 | var start int 418 | var end int 419 | 420 | for i, v := range data { 421 | if v == "--" { 422 | start = i + 1 423 | } 424 | 425 | switch v { 426 | case "--": 427 | start = i + 1 428 | case "----------": 429 | end = i - 1 430 | break 431 | } 432 | } 433 | 434 | return data[start:end], end + 2 435 | } 436 | 437 | // parseValue parses "name = value" string 438 | func parseValue(s string) (string, string) { 439 | valSlice := strings.Split(s, " = ") 440 | 441 | if len(valSlice) == 2 { 442 | return valSlice[0], valSlice[1] 443 | } 444 | 445 | return "", "" 446 | } 447 | 448 | // isExist checks if file or directory exists 449 | func isExist(t string) bool { 450 | _, err := os.Stat(t) 451 | return !os.IsNotExist(err) 452 | } 453 | 454 | // between returns value between min and max values 455 | func between(val, min, max int) int { 456 | switch { 457 | case val < min: 458 | return min 459 | case val > max: 460 | return max 461 | default: 462 | return val 463 | } 464 | } 465 | -------------------------------------------------------------------------------- /zip7_test.go: -------------------------------------------------------------------------------- 1 | package zip7 2 | 3 | // ////////////////////////////////////////////////////////////////////////////////// // 4 | // // 5 | // Copyright (c) 2024 ESSENTIAL KAOS // 6 | // Apache License, Version 2.0 // 7 | // // 8 | // ////////////////////////////////////////////////////////////////////////////////// // 9 | 10 | import ( 11 | "testing" 12 | 13 | "github.com/essentialkaos/ek/v13/fsutil" 14 | 15 | check "github.com/essentialkaos/check" 16 | ) 17 | 18 | // ////////////////////////////////////////////////////////////////////////////////// // 19 | 20 | func Test(t *testing.T) { check.TestingT(t) } 21 | 22 | // ////////////////////////////////////////////////////////////////////////////////// // 23 | 24 | type Z7Suite struct{} 25 | 26 | // ////////////////////////////////////////////////////////////////////////////////// // 27 | 28 | var _ = check.Suite(&Z7Suite{}) 29 | 30 | // ////////////////////////////////////////////////////////////////////////////////// // 31 | 32 | func (s *Z7Suite) TestListing7zNoComp(c *check.C) { 33 | info, err := List(Props{File: "testdata/test-no-compression.7z"}) 34 | 35 | c.Assert(err, check.IsNil) 36 | c.Assert(info, check.NotNil) 37 | 38 | c.Assert(info.Path, check.Equals, "testdata/test-no-compression.7z") 39 | c.Assert(info.Type, check.Equals, TYPE_7Z) 40 | c.Assert(info.Method, check.DeepEquals, []string{"Copy"}) 41 | c.Assert(info.Solid, check.Equals, false) 42 | c.Assert(info.Blocks, check.Equals, 3) 43 | c.Assert(info.PhysicalSize, check.Equals, 246) 44 | c.Assert(info.HeadersSize, check.Equals, 224) 45 | 46 | c.Assert(info.Files, check.HasLen, 6) 47 | 48 | c.Assert(info.Files[0].Path, check.Equals, "test") 49 | c.Assert(info.Files[0].Folder, check.Equals, "") 50 | c.Assert(info.Files[0].Size, check.Equals, 0) 51 | c.Assert(info.Files[0].PackedSize, check.Equals, 0) 52 | c.Assert(info.Files[0].Modified.Unix(), check.Not(check.Equals), 0) 53 | c.Assert(info.Files[0].Created.Unix() < 0, check.Equals, true) 54 | c.Assert(info.Files[0].Accessed.Unix() < 0, check.Equals, true) 55 | c.Assert(info.Files[0].Attributes, check.Equals, "D_ drwxr-xr-x") 56 | c.Assert(info.Files[0].CRC, check.Equals, 0) 57 | c.Assert(info.Files[0].Encrypted, check.Equals, false) 58 | c.Assert(info.Files[0].Method, check.DeepEquals, []string{""}) 59 | c.Assert(info.Files[0].Block, check.Equals, 0) 60 | c.Assert(info.Files[0].Comment, check.Equals, "") 61 | c.Assert(info.Files[0].HostOS, check.Equals, "") 62 | c.Assert(info.Files[0].Version, check.Equals, 0) 63 | 64 | c.Assert(info.Files[1].Path, check.Equals, "test/dir1") 65 | c.Assert(info.Files[1].Folder, check.Equals, "") 66 | c.Assert(info.Files[1].Size, check.Equals, 0) 67 | c.Assert(info.Files[1].PackedSize, check.Equals, 0) 68 | c.Assert(info.Files[1].Modified.Unix(), check.Not(check.Equals), 0) 69 | c.Assert(info.Files[1].Created.Unix() < 0, check.Equals, true) 70 | c.Assert(info.Files[1].Accessed.Unix() < 0, check.Equals, true) 71 | c.Assert(info.Files[1].Attributes, check.Equals, "D_ drwxr-xr-x") 72 | c.Assert(info.Files[1].CRC, check.Equals, 0) 73 | c.Assert(info.Files[1].Encrypted, check.Equals, false) 74 | c.Assert(info.Files[1].Method, check.DeepEquals, []string{""}) 75 | c.Assert(info.Files[1].Block, check.Equals, 0) 76 | c.Assert(info.Files[1].Comment, check.Equals, "") 77 | c.Assert(info.Files[1].HostOS, check.Equals, "") 78 | c.Assert(info.Files[1].Version, check.Equals, 0) 79 | 80 | c.Assert(info.Files[2].Path, check.Equals, "test/file2.log") 81 | c.Assert(info.Files[2].Folder, check.Equals, "") 82 | c.Assert(info.Files[2].Size, check.Equals, 0) 83 | c.Assert(info.Files[2].PackedSize, check.Equals, 0) 84 | c.Assert(info.Files[2].Modified.Unix(), check.Not(check.Equals), 0) 85 | c.Assert(info.Files[2].Created.Unix() < 0, check.Equals, true) 86 | c.Assert(info.Files[2].Accessed.Unix() < 0, check.Equals, true) 87 | c.Assert(info.Files[2].Attributes, check.Equals, "A_ -rw-r--r--") 88 | c.Assert(info.Files[2].CRC, check.Equals, 0) 89 | c.Assert(info.Files[2].Encrypted, check.Equals, false) 90 | c.Assert(info.Files[2].Method, check.DeepEquals, []string{""}) 91 | c.Assert(info.Files[2].Block, check.Equals, 0) 92 | c.Assert(info.Files[2].Comment, check.Equals, "") 93 | c.Assert(info.Files[2].HostOS, check.Equals, "") 94 | c.Assert(info.Files[2].Version, check.Equals, 0) 95 | 96 | c.Assert(info.Files[3].Path, check.Equals, "test/dir1/file1.log") 97 | c.Assert(info.Files[3].Folder, check.Equals, "") 98 | c.Assert(info.Files[3].Size, check.Equals, 11) 99 | c.Assert(info.Files[3].PackedSize, check.Equals, 11) 100 | c.Assert(info.Files[3].Modified.Unix(), check.Not(check.Equals), 0) 101 | c.Assert(info.Files[3].Created.Unix() < 0, check.Equals, true) 102 | c.Assert(info.Files[3].Accessed.Unix() < 0, check.Equals, true) 103 | c.Assert(info.Files[3].Attributes, check.Equals, "A_ -rw-r--r--") 104 | c.Assert(info.Files[3].CRC, check.Equals, 2073076399) 105 | c.Assert(info.Files[3].Encrypted, check.Equals, false) 106 | c.Assert(info.Files[3].Method, check.DeepEquals, []string{"Copy"}) 107 | c.Assert(info.Files[3].Block, check.Equals, 0) 108 | c.Assert(info.Files[3].Comment, check.Equals, "") 109 | c.Assert(info.Files[3].HostOS, check.Equals, "") 110 | c.Assert(info.Files[3].Version, check.Equals, 0) 111 | 112 | c.Assert(info.Files[4].Path, check.Equals, "test/file1.log") 113 | c.Assert(info.Files[4].Folder, check.Equals, "") 114 | c.Assert(info.Files[4].Size, check.Equals, 6) 115 | c.Assert(info.Files[4].PackedSize, check.Equals, 6) 116 | c.Assert(info.Files[4].Modified.Unix(), check.Not(check.Equals), 0) 117 | c.Assert(info.Files[4].Created.Unix() < 0, check.Equals, true) 118 | c.Assert(info.Files[4].Accessed.Unix() < 0, check.Equals, true) 119 | c.Assert(info.Files[4].Attributes, check.Equals, "A_ -rw-r--r--") 120 | c.Assert(info.Files[4].CRC, check.Equals, 3157996776) 121 | c.Assert(info.Files[4].Encrypted, check.Equals, false) 122 | c.Assert(info.Files[4].Method, check.DeepEquals, []string{"Copy"}) 123 | c.Assert(info.Files[4].Block, check.Equals, 1) 124 | c.Assert(info.Files[4].Comment, check.Equals, "") 125 | c.Assert(info.Files[4].HostOS, check.Equals, "") 126 | c.Assert(info.Files[4].Version, check.Equals, 0) 127 | 128 | c.Assert(info.Files[5].Path, check.Equals, "test/file3.log") 129 | c.Assert(info.Files[5].Folder, check.Equals, "") 130 | c.Assert(info.Files[5].Size, check.Equals, 5) 131 | c.Assert(info.Files[5].PackedSize, check.Equals, 5) 132 | c.Assert(info.Files[5].Modified.Unix(), check.Not(check.Equals), 0) 133 | c.Assert(info.Files[5].Created.Unix() < 0, check.Equals, true) 134 | c.Assert(info.Files[5].Accessed.Unix() < 0, check.Equals, true) 135 | c.Assert(info.Files[5].Attributes, check.Equals, "A_ -rw-r--r--") 136 | c.Assert(info.Files[5].CRC, check.Equals, 3168002993) 137 | c.Assert(info.Files[5].Encrypted, check.Equals, false) 138 | c.Assert(info.Files[5].Method, check.DeepEquals, []string{"Copy"}) 139 | c.Assert(info.Files[5].Block, check.Equals, 2) 140 | c.Assert(info.Files[5].Comment, check.Equals, "") 141 | c.Assert(info.Files[5].HostOS, check.Equals, "") 142 | c.Assert(info.Files[5].Version, check.Equals, 0) 143 | } 144 | 145 | func (s *Z7Suite) TestListing7zMaxComp(c *check.C) { 146 | info, err := List(Props{File: "testdata/test-max-compression.7z"}) 147 | 148 | c.Assert(err, check.IsNil) 149 | c.Assert(info, check.NotNil) 150 | 151 | c.Assert(info.Path, check.Equals, "testdata/test-max-compression.7z") 152 | c.Assert(info.Type, check.Equals, TYPE_7Z) 153 | c.Assert(info.Method, check.DeepEquals, []string{"LZMA2:12"}) 154 | c.Assert(info.Solid, check.Equals, true) 155 | c.Assert(info.Blocks, check.Equals, 1) 156 | c.Assert(info.PhysicalSize, check.Equals, 254) 157 | c.Assert(info.HeadersSize, check.Equals, 228) 158 | 159 | c.Assert(info.Files, check.HasLen, 6) 160 | 161 | c.Assert(info.Files[0].Path, check.Equals, "test") 162 | c.Assert(info.Files[0].Folder, check.Equals, "") 163 | c.Assert(info.Files[0].Size, check.Equals, 0) 164 | c.Assert(info.Files[0].PackedSize, check.Equals, 0) 165 | c.Assert(info.Files[0].Modified.Unix(), check.Not(check.Equals), 0) 166 | c.Assert(info.Files[0].Created.Unix() < 0, check.Equals, true) 167 | c.Assert(info.Files[0].Accessed.Unix() < 0, check.Equals, true) 168 | c.Assert(info.Files[0].Attributes, check.Equals, "D_ drwxr-xr-x") 169 | c.Assert(info.Files[0].CRC, check.Equals, 0) 170 | c.Assert(info.Files[0].Encrypted, check.Equals, false) 171 | c.Assert(info.Files[0].Method, check.DeepEquals, []string{""}) 172 | c.Assert(info.Files[0].Block, check.Equals, 0) 173 | c.Assert(info.Files[0].Comment, check.Equals, "") 174 | c.Assert(info.Files[0].HostOS, check.Equals, "") 175 | c.Assert(info.Files[0].Version, check.Equals, 0) 176 | 177 | c.Assert(info.Files[1].Path, check.Equals, "test/dir1") 178 | c.Assert(info.Files[1].Folder, check.Equals, "") 179 | c.Assert(info.Files[1].Size, check.Equals, 0) 180 | c.Assert(info.Files[1].PackedSize, check.Equals, 0) 181 | c.Assert(info.Files[1].Modified.Unix(), check.Not(check.Equals), 0) 182 | c.Assert(info.Files[1].Created.Unix() < 0, check.Equals, true) 183 | c.Assert(info.Files[1].Accessed.Unix() < 0, check.Equals, true) 184 | c.Assert(info.Files[1].Attributes, check.Equals, "D_ drwxr-xr-x") 185 | c.Assert(info.Files[1].CRC, check.Equals, 0) 186 | c.Assert(info.Files[1].Encrypted, check.Equals, false) 187 | c.Assert(info.Files[1].Method, check.DeepEquals, []string{""}) 188 | c.Assert(info.Files[1].Block, check.Equals, 0) 189 | c.Assert(info.Files[1].Comment, check.Equals, "") 190 | c.Assert(info.Files[1].HostOS, check.Equals, "") 191 | c.Assert(info.Files[1].Version, check.Equals, 0) 192 | 193 | c.Assert(info.Files[2].Path, check.Equals, "test/file2.log") 194 | c.Assert(info.Files[2].Folder, check.Equals, "") 195 | c.Assert(info.Files[2].Size, check.Equals, 0) 196 | c.Assert(info.Files[2].PackedSize, check.Equals, 0) 197 | c.Assert(info.Files[2].Modified.Unix(), check.Not(check.Equals), 0) 198 | c.Assert(info.Files[2].Created.Unix() < 0, check.Equals, true) 199 | c.Assert(info.Files[2].Accessed.Unix() < 0, check.Equals, true) 200 | c.Assert(info.Files[2].Attributes, check.Equals, "A_ -rw-r--r--") 201 | c.Assert(info.Files[2].CRC, check.Equals, 0) 202 | c.Assert(info.Files[2].Encrypted, check.Equals, false) 203 | c.Assert(info.Files[2].Method, check.DeepEquals, []string{""}) 204 | c.Assert(info.Files[2].Block, check.Equals, 0) 205 | c.Assert(info.Files[2].Comment, check.Equals, "") 206 | c.Assert(info.Files[2].HostOS, check.Equals, "") 207 | c.Assert(info.Files[2].Version, check.Equals, 0) 208 | 209 | c.Assert(info.Files[3].Path, check.Equals, "test/dir1/file1.log") 210 | c.Assert(info.Files[3].Folder, check.Equals, "") 211 | c.Assert(info.Files[3].Size, check.Equals, 11) 212 | c.Assert(info.Files[3].PackedSize, check.Equals, 26) 213 | c.Assert(info.Files[3].Modified.Unix(), check.Not(check.Equals), 0) 214 | c.Assert(info.Files[3].Created.Unix() < 0, check.Equals, true) 215 | c.Assert(info.Files[3].Accessed.Unix() < 0, check.Equals, true) 216 | c.Assert(info.Files[3].Attributes, check.Equals, "A_ -rw-r--r--") 217 | c.Assert(info.Files[3].CRC, check.Equals, 2073076399) 218 | c.Assert(info.Files[3].Encrypted, check.Equals, false) 219 | c.Assert(info.Files[3].Method, check.DeepEquals, []string{"LZMA2:12"}) 220 | c.Assert(info.Files[3].Block, check.Equals, 0) 221 | c.Assert(info.Files[3].Comment, check.Equals, "") 222 | c.Assert(info.Files[3].HostOS, check.Equals, "") 223 | c.Assert(info.Files[3].Version, check.Equals, 0) 224 | 225 | c.Assert(info.Files[4].Path, check.Equals, "test/file1.log") 226 | c.Assert(info.Files[4].Folder, check.Equals, "") 227 | c.Assert(info.Files[4].Size, check.Equals, 6) 228 | c.Assert(info.Files[4].PackedSize, check.Equals, 0) 229 | c.Assert(info.Files[4].Modified.Unix(), check.Not(check.Equals), 0) 230 | c.Assert(info.Files[4].Created.Unix() < 0, check.Equals, true) 231 | c.Assert(info.Files[4].Accessed.Unix() < 0, check.Equals, true) 232 | c.Assert(info.Files[4].Attributes, check.Equals, "A_ -rw-r--r--") 233 | c.Assert(info.Files[4].CRC, check.Equals, 3157996776) 234 | c.Assert(info.Files[4].Encrypted, check.Equals, false) 235 | c.Assert(info.Files[4].Method, check.DeepEquals, []string{"LZMA2:12"}) 236 | c.Assert(info.Files[4].Block, check.Equals, 0) 237 | c.Assert(info.Files[4].Comment, check.Equals, "") 238 | c.Assert(info.Files[4].HostOS, check.Equals, "") 239 | c.Assert(info.Files[4].Version, check.Equals, 0) 240 | 241 | c.Assert(info.Files[5].Path, check.Equals, "test/file3.log") 242 | c.Assert(info.Files[5].Folder, check.Equals, "") 243 | c.Assert(info.Files[5].Size, check.Equals, 5) 244 | c.Assert(info.Files[5].PackedSize, check.Equals, 0) 245 | c.Assert(info.Files[5].Modified.Unix(), check.Not(check.Equals), 0) 246 | c.Assert(info.Files[5].Created.Unix() < 0, check.Equals, true) 247 | c.Assert(info.Files[5].Accessed.Unix() < 0, check.Equals, true) 248 | c.Assert(info.Files[5].Attributes, check.Equals, "A_ -rw-r--r--") 249 | c.Assert(info.Files[5].CRC, check.Equals, 3168002993) 250 | c.Assert(info.Files[5].Encrypted, check.Equals, false) 251 | c.Assert(info.Files[5].Method, check.DeepEquals, []string{"LZMA2:12"}) 252 | c.Assert(info.Files[5].Block, check.Equals, 0) 253 | c.Assert(info.Files[5].Comment, check.Equals, "") 254 | c.Assert(info.Files[5].HostOS, check.Equals, "") 255 | c.Assert(info.Files[5].Version, check.Equals, 0) 256 | } 257 | 258 | func (s *Z7Suite) TestListingZip(c *check.C) { 259 | info, err := List(Props{File: "testdata/test.zip"}) 260 | 261 | c.Assert(err, check.IsNil) 262 | c.Assert(info, check.NotNil) 263 | 264 | c.Assert(info.Path, check.Equals, "testdata/test.zip") 265 | c.Assert(info.Type, check.Equals, TYPE_ZIP) 266 | c.Assert(info.Method, check.DeepEquals, []string{""}) 267 | c.Assert(info.Solid, check.Equals, false) 268 | c.Assert(info.Blocks, check.Equals, 0) 269 | c.Assert(info.PhysicalSize, check.Equals, 0) 270 | c.Assert(info.HeadersSize, check.Equals, 0) 271 | 272 | c.Assert(info.Files, check.HasLen, 6) 273 | 274 | c.Assert(info.Files[0].Path, check.Equals, "test") 275 | c.Assert(info.Files[0].Folder, check.Equals, "+") 276 | c.Assert(info.Files[0].Size, check.Equals, 0) 277 | c.Assert(info.Files[0].PackedSize, check.Equals, 0) 278 | c.Assert(info.Files[0].Modified.Unix(), check.Not(check.Equals), 0) 279 | c.Assert(info.Files[0].Created.Unix() < 0, check.Equals, true) 280 | c.Assert(info.Files[0].Accessed.Unix() < 0, check.Equals, true) 281 | c.Assert(info.Files[0].Attributes, check.Equals, "D_ drwxr-xr-x") 282 | c.Assert(info.Files[0].CRC, check.Equals, 0) 283 | c.Assert(info.Files[0].Encrypted, check.Equals, false) 284 | c.Assert(info.Files[0].Method, check.DeepEquals, []string{"Store"}) 285 | c.Assert(info.Files[0].Block, check.Equals, 0) 286 | c.Assert(info.Files[0].Comment, check.Equals, "") 287 | c.Assert(info.Files[0].HostOS, check.Equals, "Unix") 288 | c.Assert(info.Files[0].Version, check.Equals, 10) 289 | 290 | c.Assert(info.Files[1].Path, check.Equals, "test/file1.log") 291 | c.Assert(info.Files[1].Folder, check.Equals, "-") 292 | c.Assert(info.Files[1].Size, check.Equals, 6) 293 | c.Assert(info.Files[1].PackedSize, check.Equals, 6) 294 | c.Assert(info.Files[1].Modified.Unix(), check.Not(check.Equals), 0) 295 | c.Assert(info.Files[1].Created.Unix() < 0, check.Equals, true) 296 | c.Assert(info.Files[1].Accessed.Unix() < 0, check.Equals, true) 297 | c.Assert(info.Files[1].Attributes, check.Equals, "_ -rw-r--r--") 298 | c.Assert(info.Files[1].CRC, check.Equals, 3157996776) 299 | c.Assert(info.Files[1].Encrypted, check.Equals, false) 300 | c.Assert(info.Files[1].Method, check.DeepEquals, []string{"Store"}) 301 | c.Assert(info.Files[1].Block, check.Equals, 0) 302 | c.Assert(info.Files[1].Comment, check.Equals, "") 303 | c.Assert(info.Files[1].HostOS, check.Equals, "Unix") 304 | c.Assert(info.Files[1].Version, check.Equals, 10) 305 | 306 | c.Assert(info.Files[2].Path, check.Equals, "test/file2.log") 307 | c.Assert(info.Files[2].Folder, check.Equals, "-") 308 | c.Assert(info.Files[2].Size, check.Equals, 0) 309 | c.Assert(info.Files[2].PackedSize, check.Equals, 0) 310 | c.Assert(info.Files[2].Modified.Unix(), check.Not(check.Equals), 0) 311 | c.Assert(info.Files[2].Created.Unix() < 0, check.Equals, true) 312 | c.Assert(info.Files[2].Accessed.Unix() < 0, check.Equals, true) 313 | c.Assert(info.Files[2].Attributes, check.Equals, "_ -rw-r--r--") 314 | c.Assert(info.Files[2].CRC, check.Equals, 0) 315 | c.Assert(info.Files[2].Encrypted, check.Equals, false) 316 | c.Assert(info.Files[2].Method, check.DeepEquals, []string{"Store"}) 317 | c.Assert(info.Files[2].Block, check.Equals, 0) 318 | c.Assert(info.Files[2].Comment, check.Equals, "") 319 | c.Assert(info.Files[2].HostOS, check.Equals, "Unix") 320 | c.Assert(info.Files[2].Version, check.Equals, 10) 321 | 322 | c.Assert(info.Files[3].Path, check.Equals, "test/dir1") 323 | c.Assert(info.Files[3].Folder, check.Equals, "+") 324 | c.Assert(info.Files[3].Size, check.Equals, 0) 325 | c.Assert(info.Files[3].PackedSize, check.Equals, 0) 326 | c.Assert(info.Files[3].Modified.Unix(), check.Not(check.Equals), 0) 327 | c.Assert(info.Files[3].Created.Unix() < 0, check.Equals, true) 328 | c.Assert(info.Files[3].Accessed.Unix() < 0, check.Equals, true) 329 | c.Assert(info.Files[3].Attributes, check.Equals, "D_ drwxr-xr-x") 330 | c.Assert(info.Files[3].CRC, check.Equals, 0) 331 | c.Assert(info.Files[3].Encrypted, check.Equals, false) 332 | c.Assert(info.Files[3].Method, check.DeepEquals, []string{"Store"}) 333 | c.Assert(info.Files[3].Block, check.Equals, 0) 334 | c.Assert(info.Files[3].Comment, check.Equals, "") 335 | c.Assert(info.Files[3].HostOS, check.Equals, "Unix") 336 | c.Assert(info.Files[3].Version, check.Equals, 10) 337 | 338 | c.Assert(info.Files[4].Path, check.Equals, "test/dir1/file1.log") 339 | c.Assert(info.Files[4].Folder, check.Equals, "-") 340 | c.Assert(info.Files[4].Size, check.Equals, 11) 341 | c.Assert(info.Files[4].PackedSize, check.Equals, 11) 342 | c.Assert(info.Files[4].Modified.Unix(), check.Not(check.Equals), 0) 343 | c.Assert(info.Files[4].Created.Unix() < 0, check.Equals, true) 344 | c.Assert(info.Files[4].Accessed.Unix() < 0, check.Equals, true) 345 | c.Assert(info.Files[4].Attributes, check.Equals, "_ -rw-r--r--") 346 | c.Assert(info.Files[4].CRC, check.Equals, 2073076399) 347 | c.Assert(info.Files[4].Encrypted, check.Equals, false) 348 | c.Assert(info.Files[4].Method, check.DeepEquals, []string{"Store"}) 349 | c.Assert(info.Files[4].Block, check.Equals, 0) 350 | c.Assert(info.Files[4].Comment, check.Equals, "") 351 | c.Assert(info.Files[4].HostOS, check.Equals, "Unix") 352 | c.Assert(info.Files[4].Version, check.Equals, 10) 353 | 354 | c.Assert(info.Files[5].Path, check.Equals, "test/file3.log") 355 | c.Assert(info.Files[5].Folder, check.Equals, "-") 356 | c.Assert(info.Files[5].Size, check.Equals, 5) 357 | c.Assert(info.Files[5].PackedSize, check.Equals, 5) 358 | c.Assert(info.Files[5].Modified.Unix(), check.Not(check.Equals), 0) 359 | c.Assert(info.Files[5].Created.Unix() < 0, check.Equals, true) 360 | c.Assert(info.Files[5].Accessed.Unix() < 0, check.Equals, true) 361 | c.Assert(info.Files[5].Attributes, check.Equals, "_ -rw-r--r--") 362 | c.Assert(info.Files[5].CRC, check.Equals, 3168002993) 363 | c.Assert(info.Files[5].Encrypted, check.Equals, false) 364 | c.Assert(info.Files[5].Method, check.DeepEquals, []string{"Store"}) 365 | c.Assert(info.Files[5].Block, check.Equals, 0) 366 | c.Assert(info.Files[5].Comment, check.Equals, "") 367 | c.Assert(info.Files[5].HostOS, check.Equals, "Unix") 368 | c.Assert(info.Files[5].Version, check.Equals, 10) 369 | } 370 | 371 | func (s *Z7Suite) TestListingGz(c *check.C) { 372 | info, err := List(Props{File: "testdata/test.tar.gz"}) 373 | 374 | c.Assert(err, check.IsNil) 375 | c.Assert(info, check.NotNil) 376 | 377 | c.Assert(info.Path, check.Equals, "testdata/test.tar.gz") 378 | c.Assert(info.Type, check.Equals, TYPE_GZIP) 379 | c.Assert(info.Method, check.DeepEquals, []string{""}) 380 | c.Assert(info.Solid, check.Equals, false) 381 | c.Assert(info.Blocks, check.Equals, 0) 382 | c.Assert(info.PhysicalSize, check.Equals, 0) 383 | c.Assert(info.HeadersSize, check.Equals, 0) 384 | 385 | c.Assert(info.Files, check.HasLen, 1) 386 | 387 | c.Assert(info.Files[0].Path, check.Equals, "test.tar") 388 | c.Assert(info.Files[0].Folder, check.Equals, "") 389 | c.Assert(info.Files[0].Size, check.Equals, 10240) 390 | c.Assert(info.Files[0].PackedSize, check.Equals, 253) 391 | c.Assert(info.Files[0].Modified.Unix(), check.Not(check.Equals), 0) 392 | c.Assert(info.Files[0].Created.Unix() < 0, check.Equals, true) 393 | c.Assert(info.Files[0].Accessed.Unix() < 0, check.Equals, true) 394 | c.Assert(info.Files[0].Attributes, check.Equals, "") 395 | c.Assert(info.Files[0].CRC, check.Equals, 285968217) 396 | c.Assert(info.Files[0].Encrypted, check.Equals, false) 397 | c.Assert(info.Files[0].Method, check.DeepEquals, []string{""}) 398 | c.Assert(info.Files[0].Block, check.Equals, 0) 399 | c.Assert(info.Files[0].Comment, check.Equals, "") 400 | c.Assert(info.Files[0].HostOS, check.Equals, "Unix") 401 | c.Assert(info.Files[0].Version, check.Equals, 0) 402 | } 403 | 404 | func (s *Z7Suite) TestListingBz(c *check.C) { 405 | info, err := List(Props{File: "testdata/test.tar.bz2"}) 406 | 407 | c.Assert(err, check.IsNil) 408 | c.Assert(info, check.NotNil) 409 | 410 | c.Assert(info.Path, check.Equals, "testdata/test.tar.bz2") 411 | c.Assert(info.Type, check.Equals, TYPE_BZIP) 412 | c.Assert(info.Method, check.DeepEquals, []string{""}) 413 | c.Assert(info.Solid, check.Equals, false) 414 | c.Assert(info.Blocks, check.Equals, 0) 415 | c.Assert(info.PhysicalSize, check.Equals, 0) 416 | c.Assert(info.HeadersSize, check.Equals, 0) 417 | 418 | c.Assert(info.Files, check.HasLen, 1) 419 | 420 | c.Assert(info.Files[0].Path, check.Equals, "") 421 | c.Assert(info.Files[0].Folder, check.Equals, "") 422 | c.Assert(info.Files[0].Size, check.Equals, 0) 423 | c.Assert(info.Files[0].PackedSize, check.Equals, 0) 424 | c.Assert(info.Files[0].Modified.Unix() < 0, check.Equals, true) 425 | c.Assert(info.Files[0].Created.Unix() < 0, check.Equals, true) 426 | c.Assert(info.Files[0].Accessed.Unix() < 0, check.Equals, true) 427 | c.Assert(info.Files[0].Attributes, check.Equals, "") 428 | c.Assert(info.Files[0].CRC, check.Equals, 0) 429 | c.Assert(info.Files[0].Encrypted, check.Equals, false) 430 | c.Assert(info.Files[0].Method, check.DeepEquals, []string{""}) 431 | c.Assert(info.Files[0].Block, check.Equals, 0) 432 | c.Assert(info.Files[0].Comment, check.Equals, "") 433 | c.Assert(info.Files[0].HostOS, check.Equals, "") 434 | c.Assert(info.Files[0].Version, check.Equals, 0) 435 | } 436 | 437 | func (s *Z7Suite) TestListingXz(c *check.C) { 438 | info, err := List(Props{File: "testdata/test.tar.xz"}) 439 | 440 | c.Assert(err, check.IsNil) 441 | c.Assert(info, check.NotNil) 442 | 443 | c.Assert(info.Path, check.Equals, "testdata/test.tar.xz") 444 | c.Assert(info.Type, check.Equals, TYPE_XZ) 445 | c.Assert(info.Method, check.DeepEquals, []string{"LZMA2:26", "CRC64"}) 446 | c.Assert(info.Solid, check.Equals, false) 447 | c.Assert(info.Blocks, check.Equals, 0) 448 | c.Assert(info.PhysicalSize, check.Equals, 0) 449 | c.Assert(info.HeadersSize, check.Equals, 0) 450 | 451 | c.Assert(info.Files, check.HasLen, 1) 452 | 453 | c.Assert(info.Files[0].Path, check.Equals, "") 454 | c.Assert(info.Files[0].Folder, check.Equals, "") 455 | c.Assert(info.Files[0].Size, check.Equals, 10240) 456 | c.Assert(info.Files[0].PackedSize, check.Equals, 272) 457 | c.Assert(info.Files[0].Modified.Unix() < 0, check.Equals, true) 458 | c.Assert(info.Files[0].Created.Unix() < 0, check.Equals, true) 459 | c.Assert(info.Files[0].Accessed.Unix() < 0, check.Equals, true) 460 | c.Assert(info.Files[0].Attributes, check.Equals, "") 461 | c.Assert(info.Files[0].CRC, check.Equals, 0) 462 | c.Assert(info.Files[0].Encrypted, check.Equals, false) 463 | c.Assert(info.Files[0].Method, check.DeepEquals, []string{"LZMA2:26", "CRC64"}) 464 | c.Assert(info.Files[0].Block, check.Equals, 0) 465 | c.Assert(info.Files[0].Comment, check.Equals, "") 466 | c.Assert(info.Files[0].HostOS, check.Equals, "") 467 | c.Assert(info.Files[0].Version, check.Equals, 0) 468 | } 469 | 470 | func (s *Z7Suite) TestCheck(c *check.C) { 471 | ok, err := Check(Props{File: "testdata/test-max-compression.7z"}) 472 | 473 | c.Assert(ok, check.Equals, true) 474 | c.Assert(err, check.IsNil) 475 | 476 | ok, err = Check(Props{File: "testdata/test-broken.7z"}) 477 | 478 | c.Assert(ok, check.Equals, false) 479 | c.Assert(err, check.NotNil) 480 | 481 | ok, err = Check(Props{File: "testdata/test-not-exist.7z"}) 482 | 483 | c.Assert(ok, check.Equals, false) 484 | c.Assert(err, check.NotNil) 485 | } 486 | 487 | func (s *Z7Suite) TestAdd(c *check.C) { 488 | resultFile := c.MkDir() + "/test.7z" 489 | 490 | _, err := Add(Props{File: resultFile}) 491 | 492 | c.Assert(err, check.NotNil) 493 | 494 | _, err = Add(Props{File: resultFile}, "testdata/test") 495 | 496 | c.Assert(err, check.IsNil) 497 | 498 | c.Assert(fsutil.IsExist(resultFile), check.Equals, true) 499 | c.Assert(fsutil.IsReadable(resultFile), check.Equals, true) 500 | c.Assert(fsutil.IsEmpty(resultFile), check.Equals, false) 501 | 502 | ok, err := Check(Props{File: resultFile}) 503 | 504 | c.Assert(ok, check.Equals, true) 505 | c.Assert(err, check.IsNil) 506 | 507 | resultFile = c.MkDir() + "/test1.7z" 508 | 509 | _, err = Add(Props{File: resultFile}, "testdata/test") 510 | 511 | c.Assert(err, check.IsNil) 512 | 513 | c.Assert(fsutil.IsExist(resultFile), check.Equals, true) 514 | c.Assert(fsutil.IsReadable(resultFile), check.Equals, true) 515 | c.Assert(fsutil.IsEmpty(resultFile), check.Equals, false) 516 | 517 | ok, err = Check(Props{File: resultFile}) 518 | 519 | c.Assert(ok, check.Equals, true) 520 | c.Assert(err, check.IsNil) 521 | 522 | resultFile = c.MkDir() + "/test2.7z" 523 | 524 | _, err = Add(Props{Dir: "testdata/test", File: resultFile}, "file1.log") 525 | 526 | c.Assert(err, check.IsNil) 527 | } 528 | 529 | func (s *Z7Suite) TestExtract(c *check.C) { 530 | outputDir := c.MkDir() 531 | 532 | _, err := Extract( 533 | Props{ 534 | File: "testdata/test-max-compression.7z", 535 | OutputDir: outputDir, 536 | }, 537 | ) 538 | 539 | c.Assert(err, check.IsNil) 540 | 541 | c.Assert(fsutil.CheckPerms("DR", outputDir+"/test"), check.Equals, true) 542 | c.Assert(fsutil.CheckPerms("DR", outputDir+"/test/dir1"), check.Equals, true) 543 | c.Assert(fsutil.CheckPerms("FRS", outputDir+"/test/dir1/file1.log"), check.Equals, true) 544 | c.Assert(fsutil.CheckPerms("FRS", outputDir+"/test/file1.log"), check.Equals, true) 545 | c.Assert(fsutil.CheckPerms("FRS", outputDir+"/test/file2.log"), check.Equals, false) 546 | c.Assert(fsutil.CheckPerms("FRS", outputDir+"/test/file3.log"), check.Equals, true) 547 | } 548 | 549 | func (s *Z7Suite) TestDelete(c *check.C) { 550 | testArchive := c.MkDir() + "/test.7z" 551 | 552 | fsutil.CopyFile("testdata/test-max-compression.7z", testArchive) 553 | 554 | _, err := Delete(Props{File: testArchive}, "test/file2.log", "test/file3.log") 555 | 556 | c.Assert(err, check.IsNil) 557 | 558 | outputDir := c.MkDir() 559 | 560 | _, err = Extract( 561 | Props{ 562 | File: testArchive, 563 | OutputDir: outputDir, 564 | }, 565 | ) 566 | 567 | c.Assert(err, check.IsNil) 568 | 569 | c.Assert(fsutil.CheckPerms("DR", outputDir+"/test"), check.Equals, true) 570 | c.Assert(fsutil.CheckPerms("DR", outputDir+"/test/dir1"), check.Equals, true) 571 | c.Assert(fsutil.CheckPerms("FR", outputDir+"/test/dir1/file1.log"), check.Equals, true) 572 | c.Assert(fsutil.CheckPerms("FR", outputDir+"/test/file1.log"), check.Equals, true) 573 | c.Assert(fsutil.CheckPerms("FR", outputDir+"/test/file2.log"), check.Equals, false) 574 | c.Assert(fsutil.CheckPerms("FR", outputDir+"/test/file3.log"), check.Equals, false) 575 | 576 | _, err = Delete(Props{File: testArchive}) 577 | 578 | c.Assert(err, check.NotNil) 579 | } 580 | 581 | func (s *Z7Suite) TestValidationErrors(c *check.C) { 582 | p := Props{File: "unknown.7z"} 583 | 584 | _, err := List(p) 585 | c.Assert(err, check.NotNil) 586 | 587 | _, err = Check(p) 588 | c.Assert(err, check.NotNil) 589 | 590 | _, err = Extract(p) 591 | c.Assert(err, check.NotNil) 592 | 593 | _, err = Delete(p, "") 594 | c.Assert(err, check.NotNil) 595 | 596 | c.Assert(Props{IncludeFile: "unknown"}.Validate(false), check.NotNil) 597 | c.Assert(Props{ExcludeFile: "unknown"}.Validate(false), check.NotNil) 598 | c.Assert(Props{OutputDir: "unknown"}.Validate(false), check.NotNil) 599 | } 600 | --------------------------------------------------------------------------------