Here you can find the source of copyFileFromZipToDir(String zipFile, String fileNamePattern, File dir)
public static String copyFileFromZipToDir(String zipFile, String fileNamePattern, File dir) throws IOException
//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; } }