1 /*
2  * Copyright (C) 2019 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 package com.android.car.bugreport;
17 
18 import android.util.Log;
19 
20 import com.google.common.io.ByteStreams;
21 
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.util.Enumeration;
27 import java.util.zip.ZipEntry;
28 import java.util.zip.ZipFile;
29 import java.util.zip.ZipOutputStream;
30 
31 /** Zip utility functions. */
32 final class ZipUtils {
33     private static final String TAG = ZipUtils.class.getSimpleName();
34 
35     /** Extracts the contents of a zip file to the zip output stream. */
extractZippedFileToZipStream(File file, ZipOutputStream zipStream)36     static void extractZippedFileToZipStream(File file, ZipOutputStream zipStream) {
37         if (!file.exists()) {
38             Log.w(TAG, "File " + file + " not found");
39             return;
40         }
41         if (file.length() == 0) {
42             // If there were issues with reading from dumpstate socket, the dumpstate zip
43             // file still might be available in
44             // /data/user_de/0/com.android.shell/files/bugreports/.
45             Log.w(TAG, "Zip file " + file.getName() + " is empty, skipping.");
46             return;
47         }
48         try (ZipFile zipFile = new ZipFile(file)) {
49             Enumeration<? extends ZipEntry> entries = zipFile.entries();
50             while (entries.hasMoreElements()) {
51                 ZipEntry entry = entries.nextElement();
52                 try (InputStream stream = zipFile.getInputStream(entry)) {
53                     writeInputStreamToZipStream(entry.getName(), stream, zipStream);
54                 }
55             }
56         } catch (IOException e) {
57             Log.w(TAG, "Failed to add " + file + " to zip", e);
58         }
59     }
60 
61     /** Adds a file to the zip output stream. */
addFileToZipStream(File file, ZipOutputStream zipStream)62     static void addFileToZipStream(File file, ZipOutputStream zipStream) {
63         if (!file.exists()) {
64             Log.w(TAG, "File " + file + " not found");
65             return;
66         }
67         if (file.length() == 0) {
68             Log.w(TAG, "File " + file.getName() + " is empty, skipping.");
69             return;
70         }
71         try (FileInputStream audioInput = new FileInputStream(file)) {
72             writeInputStreamToZipStream(file.getName(), audioInput, zipStream);
73         } catch (IOException e) {
74             Log.w(TAG, "Failed to add " + file + "to the final zip");
75         }
76     }
77 
78     /** Writes a new file from input stream to the zip stream. */
writeInputStreamToZipStream( String filename, InputStream input, ZipOutputStream zipStream)79     static void writeInputStreamToZipStream(
80             String filename, InputStream input, ZipOutputStream zipStream) throws IOException {
81         ZipEntry entry = new ZipEntry(filename);
82         zipStream.putNextEntry(entry);
83         ByteStreams.copy(input, zipStream);
84         zipStream.closeEntry();
85     }
86 
ZipUtils()87     private ZipUtils() {}
88 }
89