• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.android.commands.unzip;
6 
7 import android.util.Log;
8 
9 import java.io.BufferedInputStream;
10 import java.io.BufferedOutputStream;
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.io.OutputStream;
16 import java.io.PrintStream;
17 import java.util.zip.ZipEntry;
18 import java.util.zip.ZipInputStream;
19 
20 /**
21  *  Minimal implementation of the command-line unzip utility for Android.
22  */
23 public class Unzip {
24 
25     private static final String TAG = "Unzip";
26 
main(String[] args)27     public static void main(String[] args) {
28         try {
29             (new Unzip()).run(args);
30         } catch (RuntimeException e) {
31             Log.e(TAG, e.toString());
32             System.exit(1);
33         }
34     }
35 
showUsage(PrintStream s)36     private void showUsage(PrintStream s) {
37         s.println("Usage:");
38         s.println("unzip [zipfile]");
39     }
40 
41     @SuppressWarnings("Finally")
unzip(String[] args)42     private void unzip(String[] args) {
43         ZipInputStream zis = null;
44         try {
45             String zipfile = args[0];
46             zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(zipfile)));
47             ZipEntry ze = null;
48 
49             byte[] bytes = new byte[1024];
50             while ((ze = zis.getNextEntry()) != null) {
51                 File outputFile = new File(ze.getName());
52                 if (ze.isDirectory()) {
53                     if (!outputFile.exists() && !outputFile.mkdirs()) {
54                         throw new RuntimeException(
55                                 "Failed to create directory: " + outputFile.toString());
56                     }
57                 } else {
58                     File parentDir = outputFile.getParentFile();
59                     if (!parentDir.exists() && !parentDir.mkdirs()) {
60                         throw new RuntimeException(
61                                 "Failed to create directory: " + parentDir.toString());
62                     }
63                     OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));
64                     int actual_bytes = 0;
65                     int total_bytes = 0;
66                     while ((actual_bytes = zis.read(bytes)) != -1) {
67                         out.write(bytes, 0, actual_bytes);
68                         total_bytes += actual_bytes;
69                     }
70                     out.close();
71                 }
72                 zis.closeEntry();
73             }
74 
75         } catch (IOException e) {
76             throw new RuntimeException("Error while unzipping: " + e.toString());
77         } finally {
78             try {
79                 if (zis != null) zis.close();
80             } catch (IOException e) {
81                 throw new RuntimeException("Error while closing zip: " + e.toString());
82             }
83         }
84     }
85 
run(String[] args)86     public void run(String[] args) {
87         if (args.length != 1) {
88             showUsage(System.err);
89             throw new RuntimeException("Incorrect usage.");
90         }
91 
92         unzip(args);
93     }
94 }
95 
96