Android Zip Unzip File zipFile(File source, String basePath, ZipOutputStream zos)

Here you can find the source of zipFile(File source, String basePath, ZipOutputStream zos)

Description

create date:2009- 6- 9 author:Administrator

License

Apache License

Parameter

Parameter Description
source a parameter
basePath a parameter
zos a parameter

Exception

Parameter Description
IOException an exception

Declaration

private static void zipFile(File source, String basePath,
        ZipOutputStream zos) throws Exception 

Method Source Code

//package com.java2s;
/*Copyright (C) 2012 Crow Hou (crow_hou@126.com)

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.*/

import java.io.BufferedInputStream;

import java.io.File;
import java.io.FileInputStream;

import java.io.InputStream;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class Main {
    /**//from   ww  w.j  ava 2s  .  c  om
     * 
     * create date:2009- 6- 9 author:Administrator
     * 
     * @param source
     * @param basePath
     * @param zos
     * @throws IOException
     */
    private static void zipFile(File source, String basePath,
            ZipOutputStream zos) throws Exception {
        File[] files = null;

        if (source.isDirectory()) {
            files = source.listFiles();
        } else {
            files = new File[1];
            files[0] = source;
        }

        String pathName;
        byte[] buf = new byte[1024];
        int length = 0;

        for (File file : files) {
            if (file.isDirectory()) {
                pathName = file.getPath().substring(basePath.length() + 1)
                        + "/";
                zos.putNextEntry(new ZipEntry(pathName));
                zipFile(file, basePath, zos);
            } else {
                pathName = file.getPath().substring(basePath.length() + 1);
                InputStream is = null;
                try {
                    is = new BufferedInputStream(new FileInputStream(file));
                    zos.putNextEntry(new ZipEntry(pathName));
                    while ((length = is.read(buf)) != -1) {
                        zos.write(buf, 0, length);
                    }
                } finally {
                    is.close();
                }
            }
        }

    }
}

Related

  1. zip(String[] files, String zipFile)
  2. zip(String[] files, String zipFile)
  3. zip(File srcFile, File targetFile)
  4. zip(File[] _files, File _zipFile)
  5. zip(List listFiles, String destZipFilePath)
  6. zipFileAtPath(File sourceFile, String toLocation)
  7. decompress(File file)
  8. decompress(File file)
  9. decompress(File file, boolean delete)