Here you can find the source of unzip(File epubfile, File destination)
Parameter | Description |
---|---|
epubfile | the EPUB file |
destination | the destination folder |
Parameter | Description |
---|---|
FileNotFoundException | when EPUB file does not exist |
IOException | if the operation was unsuccessful |
public static void unzip(File epubfile, File destination) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2011-2014 Torkild U. Resheim. * * All rights reserved. This program and the accompanying materials are made * available under the terms of the Eclipse Public License v1.0 which * accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors://w w w. ja v a2s . co m * Torkild U. Resheim - initial API and implementation *******************************************************************************/ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class Main { static final int BUFFERSIZE = 2048; /** * Unpacks the given <i>epubfile</i> to the <i>destination</i> directory. This method will also validate the first * item contained in the EPUB (see {@link #writeEPUBHeader(ZipOutputStream)}). * <p> * If the destination folder does not already exist it will be created. Additionally the modification time stamp of * this folder will be set to the same as the originating EPUB file. * </p> * TODO: Actually validate the mimetype file * * @param epubfile * the EPUB file * @param destination * the destination folder * @throws FileNotFoundException * when EPUB file does not exist * @throws IOException * if the operation was unsuccessful */ public static void unzip(File epubfile, File destination) throws IOException { if (!destination.exists()) { if (!destination.mkdirs()) { throw new IOException( "Could not create directory for EPUB contents"); //$NON-NLS-1$ } } ZipInputStream in = new ZipInputStream( new FileInputStream(epubfile)); byte[] buf = new byte[BUFFERSIZE]; ZipEntry entry = null; while ((entry = in.getNextEntry()) != null) { // for each entry to be extracted String entryName = entry.getName(); File newFile = new File(destination.getAbsolutePath() + File.separator + entryName); if (entry.isDirectory()) { newFile.mkdirs(); if (entry.getTime() > 0) { newFile.setLastModified(entry.getTime()); } continue; } else { newFile.getParentFile().mkdirs(); } int n; FileOutputStream fileoutputstream = new FileOutputStream( newFile); while ((n = in.read(buf, 0, BUFFERSIZE)) > -1) { fileoutputstream.write(buf, 0, n); } fileoutputstream.close(); in.closeEntry(); // Update the file modification time if (entry.getTime() > 0) { newFile.setLastModified(entry.getTime()); } } // iterate over contents in.close(); destination.setLastModified(epubfile.lastModified()); } }