Android OutputStream Write outputSubFile(InputStream is, String subFile, String targetPath, String filename)

Here you can find the source of outputSubFile(InputStream is, String subFile, String targetPath, String filename)

Description

output Sub File

Declaration

public static boolean outputSubFile(InputStream is, String subFile,
            String targetPath, String filename) 

Method Source Code

//package com.java2s;

import java.io.BufferedOutputStream;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import java.util.List;

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];
    private static List<Object> mTmpBuffer = new ArrayList<Object>();

    public static boolean outputSubFile(InputStream is, String subFile,
            String targetPath, String filename) {
        if (is == null || targetPath.equals("") || targetPath == null
                || filename.equals("") || filename == null
                || subFile.equals("") || subFile == null) {
            return false;
        }//from w  w  w .  j  av  a  2s .c  o m
        ZipInputStream in = new ZipInputStream(is);
        ZipEntry entry = null;
        try {
            while ((entry = in.getNextEntry()) != null) {
                if (entry.getName().equals(subFile) == true
                        && entry.isDirectory() == false) {
                    checkDirectory(targetPath);
                    doOutputFile(in, targetPath + filename);
                    in.closeEntry();
                    return true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        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 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);
    }
}

Related

  1. writeByte(final String str, final OutputStream out, Map cache)
  2. uint32ToStream(long value, OutputStream stream)
  3. uint64ToStream(long value, OutputStream stream)
  4. writeFile(InputStream inputStream, OutputStream outputStream)
  5. putLong(OutputStream out, final long value, final int fitInBytes)