Here you can find the source of unzipFileInArchive(FileChannel channel, String relpathInZip, File extractTo)
public static void unzipFileInArchive(FileChannel channel, String relpathInZip, File extractTo) throws FileNotFoundException, IOException
//package com.java2s; //License from project: Apache License import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main { public static void unzipFileInArchive(FileChannel channel, String relpathInZip, File extractTo) throws FileNotFoundException, IOException { relpathInZip = relpathInZip.replace("\\", "/"); Pattern pattern = Pattern.compile(wildcardToRegex(relpathInZip), Pattern.CASE_INSENSITIVE); // ZipFile archive = new ZipFile(zip); ZipInputStream stream = new ZipInputStream(Channels.newInputStream(channel)); ZipEntry entry;/*w w w.j a va2s .c om*/ // Enumeration<ZipEntry> e = (Enumeration<ZipEntry>) archive.entries(); // while (e.hasMoreElements()) { while ((entry = stream.getNextEntry()) != null) { // ZipEntry entry = e.nextElement(); File file = new File(extractTo, entry.getName()); String name = '/' + entry.getName(); Matcher matcher = pattern.matcher(name); if (!entry.isDirectory()) { if (matcher.find() || relpathInZip.indexOf(entry.getName()) >= 0) { if (!file.getParentFile().exists()) { file.getParentFile().mkdirs(); } // InputStream in = archive.getInputStream(entry); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); byte[] buffer = new byte[8192]; int read; while (-1 != (read = stream.read(buffer))) { out.write(buffer, 0, read); } // in.close(); out.close(); } } } stream.close(); } public static String wildcardToRegex(String wildcard) { StringBuffer s = new StringBuffer(wildcard.length()); s.append('^'); for (int i = 0, is = wildcard.length(); i < is; i++) { char c = wildcard.charAt(i); switch (c) { case '*': s.append(".*"); break; case '?': s.append("."); break; // escape special regexp-characters case '(': case ')': case '[': case ']': case '$': case '^': case '.': case '{': case '}': case '|': case '\\': s.append("\\"); s.append(c); break; default: s.append(c); break; } } s.append('$'); return (s.toString()); } }