Android examples for File Input Output:Zip File
Unzip a inputstream data to the target path.
//package com.java2s; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main { private static final int BUFFER_SIZE = 8192; protected static byte buf[] = new byte[BUFFER_SIZE]; /**/*from w w w .ja v a 2s.c o m*/ * Unzip a inputstream data to the target path. if the path is exists, and * is not a dir will delete the path, and recreate it. if the path is not * exists, create the path. if the inputstream is not a package contain some * files and dirs, will out put the data to the target and create all the * files and dirs and the targetPath. * * @param is * is InputStream contain the files data. * @param targetPath * targetPath must be end with system path seprator * @return true if unzip successflly, false if failed. false if the * inputstream is null or the targetPath is null or the targetPath * is empty string. */ public static boolean UnZipFile(InputStream is, String targetPath) { if (is == null || targetPath.equals("") || targetPath == null) { return false; } try { checkDirectory(targetPath); extZipFile(is, targetPath); is.close(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } /** * Unzip a source to the targetPath, is similar with the * UnZipFile(InputStream is, String targetPath) * * @param source * source file to unzip * @param targetPath * targetPath to place out files. * @return true if successfully, false if failed. false if the source file * is not exists. */ public static boolean UnZipFile(String source, String targetPath) { File file = new File(source); if (file.exists() == false) { return false; } try { return UnZipFile(new FileInputStream(file), targetPath); } catch (Exception e) { } return false; } private static boolean checkDirectory(String dir) { File dirObj = new File(dir); if (dirObj.exists()) { if (!dirObj.isDirectory()) { dirObj.delete(); } return false; } if (!dirObj.exists()) { dirObj.mkdirs(); } return true; } private static void extZipFile(InputStream is, String extPlace) { ZipInputStream in = new ZipInputStream(is); ZipEntry entry = null; try { while ((entry = in.getNextEntry()) != null) { final String fullName = extPlace + entry.getName(); if (entry.isDirectory()) { File file = new File(fullName); file.mkdirs(); } else { doOutputFile(in, fullName); in.closeEntry(); } } } catch (IOException e) { } finally { in = null; } } /** * get the file list in the inputstream data * * @param is the inputstream data * @return the file list contain in the inputstream. if the is is null * return null */ // public static List<String> getZipFileList(InputStream is) { // List<String> ret = new ArrayList<String>(); // try { // ZipInputStream in = new ZipInputStream(is); // ZipEntry entry = null; // while ((entry = in.getNextEntry()) != null) { // ret.add(entry.getName()); // } // return ret; // } catch (Exception e) { // e.printStackTrace(); // } // return null; // } // private static void copy(byte[] src, byte[] dest, int offset, int length) { // for (int index = 0; index < length; ++index) { // dest[offset + index] = src[index]; // } // } private static void doOutputFile(InputStream is, String filename) throws IOException, FileNotFoundException { FileOutputStream os = new FileOutputStream(filename); BufferedOutputStream bos = new BufferedOutputStream(os, BUFFER_SIZE); int len; while ((len = is.read(buf, 0, BUFFER_SIZE)) > 0) { bos.write(buf, 0, len); } bos.flush(); bos.close(); os.close(); // mTmpBuffer.add(os); // mTmpBuffer.add(bos); } }