generated from moul/golang-repo-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from moul/dev/moul/apk
- Loading branch information
Showing
10 changed files
with
460 additions
and
28 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
moul.io/pkgman dependencies: (generated by github.com/tailscale/depaware) | ||
|
||
💣 github.com/avast/apkparser from moul.io/pkgman/pkg/apk | ||
github.com/basgys/goxml2json from moul.io/pkgman/pkg/apk | ||
github.com/klauspost/compress/flate from github.com/avast/apkparser | ||
github.com/peterbourgon/ff/v3 from github.com/peterbourgon/ff/v3/ffcli | ||
github.com/peterbourgon/ff/v3/ffcli from moul.io/pkgman | ||
💣 howett.net/plist from moul.io/pkgman/pkg/ipa | ||
moul.io/pkgman/pkg/apk from moul.io/pkgman | ||
moul.io/pkgman/pkg/ipa from moul.io/pkgman | ||
moul.io/u from moul.io/pkgman | ||
golang.org/x/net/html from golang.org/x/net/html/charset | ||
golang.org/x/net/html/atom from golang.org/x/net/html | ||
golang.org/x/net/html/charset from github.com/basgys/goxml2json | ||
golang.org/x/text/encoding from golang.org/x/net/html/charset+ | ||
golang.org/x/text/encoding/charmap from golang.org/x/net/html/charset+ | ||
golang.org/x/text/encoding/htmlindex from golang.org/x/net/html/charset | ||
golang.org/x/text/encoding/internal from golang.org/x/text/encoding/charmap+ | ||
golang.org/x/text/encoding/japanese from golang.org/x/text/encoding/htmlindex | ||
golang.org/x/text/encoding/korean from golang.org/x/text/encoding/htmlindex | ||
golang.org/x/text/encoding/simplifiedchinese from golang.org/x/text/encoding/htmlindex | ||
golang.org/x/text/encoding/traditionalchinese from golang.org/x/text/encoding/htmlindex | ||
golang.org/x/text/encoding/unicode from golang.org/x/text/encoding/htmlindex | ||
golang.org/x/text/language from golang.org/x/text/encoding/htmlindex | ||
golang.org/x/text/runes from golang.org/x/text/encoding/unicode | ||
golang.org/x/text/transform from golang.org/x/net/html/charset+ | ||
archive/zip from github.com/avast/apkparser+ | ||
bufio from archive/zip+ | ||
bytes from bufio+ | ||
compress/flate from archive/zip | ||
context from github.com/peterbourgon/ff/v3/ffcli+ | ||
crypto from crypto/sha1 | ||
crypto/sha1 from moul.io/u | ||
encoding from encoding/json+ | ||
encoding/base64 from encoding/json+ | ||
encoding/binary from archive/zip+ | ||
encoding/hex from howett.net/plist | ||
encoding/json from github.com/peterbourgon/ff/v3+ | ||
encoding/xml from github.com/avast/apkparser+ | ||
errors from archive/zip+ | ||
flag from github.com/peterbourgon/ff/v3+ | ||
fmt from compress/flate+ | ||
hash from archive/zip+ | ||
hash/crc32 from archive/zip+ | ||
io from archive/zip+ | ||
io/ioutil from archive/zip+ | ||
log from moul.io/pkgman | ||
math from compress/flate+ | ||
math/bits from compress/flate+ | ||
mime from golang.org/x/net/html/charset | ||
os from archive/zip+ | ||
os/exec from moul.io/u | ||
os/signal from moul.io/u | ||
path from archive/zip+ | ||
path/filepath from io/ioutil+ | ||
reflect from encoding/binary+ | ||
runtime/debug from github.com/avast/apkparser | ||
sort from compress/flate+ | ||
strconv from compress/flate+ | ||
strings from archive/zip+ | ||
sync from archive/zip+ | ||
sync/atomic from context+ | ||
syscall from internal/poll+ | ||
text/tabwriter from github.com/peterbourgon/ff/v3/ffcli | ||
time from archive/zip+ | ||
unicode from bytes+ | ||
unicode/utf16 from encoding/json+ | ||
unicode/utf8 from archive/zip+ | ||
unsafe from github.com/avast/apkparser+ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package apk | ||
|
||
import ( | ||
"archive/zip" | ||
"bufio" | ||
"bytes" | ||
"encoding/xml" | ||
"fmt" | ||
"io/ioutil" | ||
"strings" | ||
|
||
"github.com/avast/apkparser" | ||
xml2json "github.com/basgys/goxml2json" | ||
) | ||
|
||
type Package struct { | ||
r *zip.ReadCloser | ||
} | ||
|
||
func Open(path string) (*Package, error) { | ||
r, err := zip.OpenReader(path) | ||
if err != nil { | ||
return nil, fmt.Errorf("open path: %w", err) | ||
} | ||
return &Package{r: r}, nil | ||
} | ||
|
||
func (p *Package) Close() error { | ||
return nil | ||
} | ||
|
||
func (p Package) Files() []*zip.File { | ||
return p.r.File | ||
} | ||
|
||
func (p Package) File(name string) *zip.File { | ||
for _, f := range p.r.File { | ||
if f.Name == name { | ||
return f | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (p Package) FileBytes(name string) ([]byte, error) { | ||
f := p.File(name) | ||
if f == nil { | ||
return nil, fmt.Errorf("no such file: %s", name) | ||
} | ||
|
||
r, err := f.Open() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return ioutil.ReadAll(r) | ||
} | ||
|
||
func (p Package) ManifestXML() (string, error) { | ||
b, err := p.FileBytes("AndroidManifest.xml") | ||
if err != nil { | ||
return "", err | ||
} | ||
binbuf := bytes.NewBuffer(b) | ||
|
||
xmlbuf := bytes.Buffer{} | ||
xmlwriter := bufio.NewWriter(&xmlbuf) | ||
enc := xml.NewEncoder(xmlwriter) | ||
enc.Indent(" ", " ") | ||
err = apkparser.ParseXml(binbuf, enc, nil) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return xmlbuf.String(), nil | ||
} | ||
|
||
func (p Package) ManifestJSON() (string, error) { | ||
xmlStr, err := p.ManifestXML() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
xml := strings.NewReader(xmlStr) | ||
json, err := xml2json.Convert(xml) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return json.String(), nil | ||
} | ||
|
||
func (p Package) Manifest() (*Manifest, error) { | ||
xmlStr, err := p.ManifestXML() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var manifest Manifest | ||
err = xml.Unmarshal([]byte(xmlStr), &manifest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &manifest, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package apk_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"moul.io/pkgman/pkg/apk" | ||
) | ||
|
||
func ExampleOpen() { | ||
pkg, err := apk.Open("path/to/apk") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer pkg.Close() | ||
|
||
fmt.Printf("Loaded APK: %v\n", pkg) | ||
} | ||
|
||
func TestOpen_gomobileipfsdemo(t *testing.T) { | ||
testdata := "../../testdata/gomobile-ipfs-demo-1.2.1.apk" | ||
pkg, err := apk.Open(testdata) | ||
if err != nil { | ||
t.Skipf("cannot open testdata %q, skipping", testdata) | ||
} | ||
require.NoError(t, err) | ||
defer pkg.Close() | ||
require.Len(t, pkg.Files(), 451) | ||
|
||
manifest, err := pkg.Manifest() | ||
require.NoError(t, err) | ||
require.Equal(t, manifest.Package, "null.example") | ||
require.Equal(t, manifest.MainActivity().Label, "Gomobile IPFS Example") | ||
require.Equal(t, manifest.MainActivity().Name, "ipfs.gomobile.example.MainActivity") | ||
|
||
noExists, err := pkg.FileBytes("blahblah") | ||
require.Nil(t, noExists) | ||
require.Error(t, err) | ||
|
||
exists, err := pkg.FileBytes("resources.arsc") | ||
require.NoError(t, err) | ||
require.Equal(t, len(exists), 257156) | ||
} |
Oops, something went wrong.