blob: 43c8d75e2aa5f9a456a2139a88c7507df0b124b1 [file] [log] [blame]
Zhomart Mukhamejanovf4d280c2018-04-17 13:20:22 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.example.android.systemupdatersample.util;
18
19import android.annotation.TargetApi;
20import android.os.Build;
21
22import com.example.android.systemupdatersample.PayloadSpec;
23
24import java.io.BufferedReader;
25import java.io.File;
26import java.io.IOException;
27import java.io.InputStream;
28import java.io.InputStreamReader;
29import java.util.ArrayList;
30import java.util.Arrays;
31import java.util.Enumeration;
32import java.util.List;
33import java.util.zip.ZipEntry;
34import java.util.zip.ZipFile;
35
36/** The helper class that creates {@link PayloadSpec}. */
37@TargetApi(Build.VERSION_CODES.N)
38public final class PayloadSpecs {
39
40 /**
41 * The payload PAYLOAD_ENTRY is stored in the zip package to comply with the Android OTA package
42 * format. We want to find out the offset of the entry, so that we can pass it over to the A/B
43 * updater without making an extra copy of the payload.
44 *
45 * <p>According to Android docs, the entries are listed in the order in which they appear in the
46 * zip file. So we enumerate the entries to identify the offset of the payload file.
47 * http://developer.android.com/reference/java/util/zip/ZipFile.html#entries()
48 */
49 public static PayloadSpec forNonStreaming(File packageFile) throws IOException {
50 boolean payloadFound = false;
51 long payloadOffset = 0;
52 long payloadSize = 0;
53
54 List<String> properties = new ArrayList<>();
55 try (ZipFile zip = new ZipFile(packageFile)) {
56 Enumeration<? extends ZipEntry> entries = zip.entries();
57 long offset = 0;
58 while (entries.hasMoreElements()) {
59 ZipEntry entry = entries.nextElement();
60 String name = entry.getName();
61 // Zip local file header has 30 bytes + filename + sizeof extra field.
62 // https://en.wikipedia.org/wiki/Zip_(file_format)
63 long extraSize = entry.getExtra() == null ? 0 : entry.getExtra().length;
64 offset += 30 + name.length() + extraSize;
65
66 if (entry.isDirectory()) {
67 continue;
68 }
69
70 long length = entry.getCompressedSize();
71 if (PackagePropertyFiles.PAYLOAD_BINARY_FILE_NAME.equals(name)) {
72 if (entry.getMethod() != ZipEntry.STORED) {
73 throw new IOException("Invalid compression method.");
74 }
75 payloadFound = true;
76 payloadOffset = offset;
77 payloadSize = length;
78 } else if (PackagePropertyFiles.PAYLOAD_PROPERTIES_FILE_NAME.equals(name)) {
79 InputStream inputStream = zip.getInputStream(entry);
80 if (inputStream != null) {
81 BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
82 String line;
83 while ((line = br.readLine()) != null) {
84 properties.add(line);
85 }
86 }
87 }
88 offset += length;
89 }
90 }
91
92 if (!payloadFound) {
93 throw new IOException("Failed to find payload entry in the given package.");
94 }
95 return PayloadSpec.newBuilder()
96 .url("file://" + packageFile.getAbsolutePath())
97 .offset(payloadOffset)
98 .size(payloadSize)
99 .properties(properties)
100 .build();
101 }
102
103 /**
104 * Converts an {@link PayloadSpec} to a string.
105 */
106 public static String toString(PayloadSpec payloadSpec) {
107 return "<PayloadSpec url=" + payloadSpec.getUrl()
108 + ", offset=" + payloadSpec.getOffset()
109 + ", size=" + payloadSpec.getSize()
110 + ", properties=" + Arrays.toString(
111 payloadSpec.getProperties().toArray(new String[0]))
112 + ">";
113 }
114
115 private PayloadSpecs() {}
116
117}