PageRenderTime 41ms CodeModel.GetById 12ms app.highlight 21ms RepoModel.GetById 2ms app.codeStats 0ms

/androidsays/src/com/google/marvin/androidsays/Unzipper.java

http://eyes-free.googlecode.com/
Java | 116 lines | 82 code | 11 blank | 23 comment | 7 complexity | ab0edfd4ac9c3826c81a6db6d74499d7 MD5 | raw file
  1/*
  2 * Copyright (C) 2008 Google Inc.
  3 * 
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * 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, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16package com.google.marvin.androidsays;
 17
 18import java.io.File;
 19import java.io.FileOutputStream;
 20import java.io.IOException;
 21import java.io.InputStream;
 22import java.util.Enumeration;
 23import java.util.zip.ZipEntry;
 24import java.net.MalformedURLException;
 25import java.net.URL;
 26import java.net.URLConnection;
 27import java.util.zip.ZipFile;
 28
 29/**
 30 * Unzips theme files for mem into the appropriate directory on the sdcard.
 31 * 
 32 * @author clchen@google.com (Charles L. Chen)
 33 */
 34public class Unzipper {
 35
 36  public static String download(String fileUrl) {
 37    URLConnection cn;
 38    try {
 39      fileUrl = (new URL(new URL(fileUrl), fileUrl)).toString();
 40      URL url = new URL(fileUrl);
 41      cn = url.openConnection();
 42      cn.connect();
 43      InputStream stream = cn.getInputStream();
 44
 45      File outputDir = new File("/sdcard/mem/memes/");
 46      outputDir.mkdirs();
 47      String filename = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
 48      filename = filename.substring(0, filename.indexOf("meme.zip") + 8);
 49
 50      File outputFile = new File("/sdcard/mem/memes/", filename);
 51      outputFile.createNewFile();
 52      FileOutputStream out = new FileOutputStream(outputFile);
 53
 54      byte buf[] = new byte[16384];
 55      do {
 56        int numread = stream.read(buf);
 57        if (numread <= 0) {
 58          break;
 59        } else {
 60          out.write(buf, 0, numread);
 61        }
 62      } while (true);
 63
 64      stream.close();
 65      out.close();
 66      return "/sdcard/mem/memes/" + filename;
 67
 68    } catch (MalformedURLException e) {
 69      // TODO Auto-generated catch block
 70      e.printStackTrace();
 71      return "";
 72    } catch (IOException e) {
 73      // TODO Auto-generated catch block
 74      e.printStackTrace();
 75      return "";
 76    }
 77  }
 78
 79  public static void unzip(String fileUrl) {
 80    try {
 81      String filename = download(fileUrl);
 82      ZipFile zip = new ZipFile(filename);
 83      Enumeration<? extends ZipEntry> zippedFiles = zip.entries();
 84      while (zippedFiles.hasMoreElements()) {
 85        ZipEntry entry = zippedFiles.nextElement();
 86        InputStream is = zip.getInputStream(entry);
 87        String name = entry.getName();
 88        File outputFile = new File("/sdcard/mem/themes/" + name);
 89        String outputPath = outputFile.getCanonicalPath();
 90        name = outputPath.substring(outputPath.lastIndexOf("/") + 1);
 91        outputPath = outputPath.substring(0, outputPath.lastIndexOf("/"));
 92        File outputDir = new File(outputPath);
 93        outputDir.mkdirs();
 94        outputFile = new File(outputPath, name);
 95        outputFile.createNewFile();
 96        FileOutputStream out = new FileOutputStream(outputFile);
 97
 98        byte buf[] = new byte[16384];
 99        do {
100          int numread = is.read(buf);
101          if (numread <= 0) {
102            break;
103          } else {
104            out.write(buf, 0, numread);
105          }
106        } while (true);
107
108        is.close();
109        out.close();
110      }
111    } catch (IOException e) {
112      // TODO Auto-generated catch block
113      e.printStackTrace();
114    }
115  }
116}