Java ZipEntry Read getZipEntry(File f, String nameToRead, String fileName)

Here you can find the source of getZipEntry(File f, String nameToRead, String fileName)

Description

Get an entry from the specified zip file

License

Apache License

Parameter

Parameter Description
f - zip file to read from.
nameToRead - name of the file to read in from the zip file
fileName - name to give the file to create

Exception

Parameter Description
IOException an exception

Declaration

public static File getZipEntry(File f, String nameToRead, String fileName) throws IOException 

Method Source Code

//package com.java2s;
/**  //from w w  w. j  a va  2s .  c om
   Copyright 2008 University of Rochester
    
   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.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class Main {
    /**
     * Get an entry from the specified zip file
     * 
     * @param f  - zip file to read from.
     * @param nameToRead - name of the file to read in  from the zip file
     * @param fileName - name to give the file to create
     * 
     * @return- an in memory file.
     * @throws IOException
     */
    public static File getZipEntry(File f, String nameToRead, String fileName) throws IOException {
        byte[] fileBytes = new byte[1024];
        File fileOut = new File(fileName);
        FileOutputStream outStream = null;
        FileInputStream fileInputStream = null;
        ZipInputStream zipInputStream = null;
        OutputStream out = null;
        ZipEntry temp;

        try {
            outStream = new FileOutputStream(fileOut);
            fileInputStream = new FileInputStream(f);
            zipInputStream = new ZipInputStream(fileInputStream);
            out = new BufferedOutputStream(outStream);

            boolean found = false;
            while (!found && (temp = zipInputStream.getNextEntry()) != null) {
                System.out.println("entry = " + temp.getName());
                System.out.println("name to Read = " + nameToRead);
                System.out.println("equals = " + temp.getName().equals(nameToRead));
                if (temp.getName().equals(nameToRead)) {
                    found = true;
                }
            }

            // write the data to the file
            if (found) {
                int amountRead = 0;
                while ((amountRead = zipInputStream.read(fileBytes)) != -1) {
                    out.write(fileBytes, 0, amountRead);
                }
                out.flush();
            }
        } finally {
            if (out != null) {
                out.close();
            }
            if (outStream != null) {
                outStream.close();
            }

            if (zipInputStream != null) {
                zipInputStream.close();
            }

            if (fileInputStream != null) {
                fileInputStream.close();
            }
            fileBytes = null;
        }

        return fileOut;

    }
}

Related

  1. getNextZipEntry(ZipInputStream in)
  2. getSimpleName(ZipEntry entry)
  3. getZipEntry(final ZipFile zipFile, final CharSequence zippedName)
  4. getZipEntry(String name, ZipFile zipFile)
  5. getZipEntry(String zipEntryName)
  6. getZipEntry(String zipFileLoc)