Java BufferedInputStream Copy copyFileFromZipToDir(String zipFile, String fileNamePattern, File dir)

Here you can find the source of copyFileFromZipToDir(String zipFile, String fileNamePattern, File dir)

Description

copy File From Zip To Dir

License

Apache License

Declaration

public static String copyFileFromZipToDir(String zipFile, String fileNamePattern, File dir) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w  w w. ja  v  a2  s  .  c o  m
 * Copyright 2017 Hortonworks.
 *
 * 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.BufferedOutputStream;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.util.Enumeration;

import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Main {
    public static String copyFileFromZipToDir(String zipFile, String fileNamePattern, File dir) throws IOException {
        ZipFile zip = new ZipFile(zipFile);
        Enumeration zipFileEntries = zip.entries();
        int BUFFER = 2048;
        while (zipFileEntries.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();
            if (currentEntry.matches(fileNamePattern)) {
                String[] currentEntrySegments = currentEntry.split(File.separator);
                String matchedFileName = currentEntrySegments[currentEntrySegments.length - 1];
                File file = new File(dir, matchedFileName);
                FileOutputStream fos = new FileOutputStream(file);
                BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
                byte data[] = new byte[BUFFER];
                BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry));
                int currentByte;
                while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, currentByte);
                }
                dest.flush();
                dest.close();
                is.close();
                return matchedFileName;
            }
        }
        return null;
    }
}

Related

  1. copyFile(String source, String destination)
  2. copyFile(String sourceFilePath, String destinationFilePath)
  3. copyFile(String src, String dest)
  4. copyFileBytes(String srcFileName, String tarFileName)
  5. copyFileFromStream(InputStream in, File dest)
  6. copyFileNormal(File copyFrom, File copyTo)
  7. copyFileToFile(File in, File out)
  8. copyFileToStream(File from, OutputStream out)
  9. copyFileToStream(File input, OutputStream os)