Can't Unzip EPub File
Asked Answered
J

3

3

IMO, I thought that epub is a kind of zip . Thus, I've tried to unzip in a way.

public class Main {
    public static void main(String argv[ ])  {
        final int BUFFER = 2048;

    try {
        BufferedOutputStream dest = null;
        FileInputStream fis = new FileInputStream("/Users/yelinaung/Documents/unzip/epub/doyle.epub");
        ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            System.out.println("Extracting: " + entry);
            int count;
            byte data[] = new byte[BUFFER];
            // write the files to the disk
            FileOutputStream fos = new FileOutputStream("/Users/yelinaung/Documents/unzip/xml/");
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = zis.read(data, 0, BUFFER))
                    != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
        }
        zis.close();
    } catch ( IOException e) {
        e.printStackTrace();
    } 
  }
}

I got that following error ..

java.io.FileNotFoundException: /Users/yelinaung/Documents/unzip/xml (No such file or directory)

though I've created a folder in that way .. and

my way of unzipping epub is right way ? .. Correct Me

Jammiejammin answered 30/6, 2010 at 9:1 Comment(0)
J
5

I've got the answer.. I haven't checked that the object that have to be extracted is folder or not .

I've corrected in this way .

public static void main(String[] args) throws IOException {
        ZipFile zipFile = new ZipFile("/Users/yelinaung/Desktop/unzip/epub/doyle.epub");
        String path = "/Users/yelinaung/Desktop/unzip/xml/";

        Enumeration files = zipFile.entries();

        while (files.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) files.nextElement();
            if (entry.isDirectory()) {
                File file = new File(path + entry.getName());
                file.mkdir();
                System.out.println("Create dir " + entry.getName());
            } else {
                File f = new File(path + entry.getName());
                FileOutputStream fos = new FileOutputStream(f);
                InputStream is = zipFile.getInputStream(entry);
                byte[] buffer = new byte[1024];
                int bytesRead = 0;
                while ((bytesRead = is.read(buffer)) != -1) {
                    fos.write(buffer, 0, bytesRead);
                }
                fos.close();
                System.out.println("Create File " + entry.getName());
            }
        }
    }

Then, got it .

Jammiejammin answered 2/7, 2010 at 4:2 Comment(0)
S
3

You are creating a FileOutputStream with the same name for every file inside your epub file.

The FileNotFoundException is thrown if the file exists and it is a directory. The first time you pass through the loop, the directory is created and in the next time, the exception is raised.

If you want to change the directory where the epub is extracted, you should do something like this:

FileOutputStream fos = new FileOutputStream("/Users/yelinaung/Documents/unzip/xml/" + entry.getName())

Update
Creating the folder before to extract each file I was able to open the epub file

...
byte data[] = new byte[BUFFER];

String path = entry.getName();
if (path.lastIndexOf('/') != -1) {
    File d = new File(path.substring(0, path.lastIndexOf('/')));
    d.mkdirs();
}

// write the files to the disk
FileOutputStream fos = new FileOutputStream(path);
...

And to change the folder where the files are extracted, just change the line where the path is defined

String path = "newFolder/" + entry.getName();
Schwejda answered 1/7, 2010 at 3:31 Comment(1)
eahh.. I've tried .. and got the same error again.. it can extract the files.. but can't folders .. the META-INF is being extracted as a file.. actually, it's a sub-folder of .epub file.Jammiejammin
M
1

This method worked fine for me,

public void doUnzip(String inputZip, String destinationDirectory)
        throws IOException {
    int BUFFER = 2048;
    List zipFiles = new ArrayList();
    File sourceZipFile = new File(inputZip);
    File unzipDestinationDirectory = new File(destinationDirectory);
    unzipDestinationDirectory.mkdir();

    ZipFile zipFile;
    // Open Zip file for reading
    zipFile = new ZipFile(sourceZipFile, ZipFile.OPEN_READ);

    // Create an enumeration of the entries in the zip file
    Enumeration zipFileEntries = zipFile.entries();

    // Process each entry
    while (zipFileEntries.hasMoreElements()) {
        // grab a zip file entry
        ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();

        String currentEntry = entry.getName();

        File destFile = new File(unzipDestinationDirectory, currentEntry);

        if (currentEntry.endsWith(".zip")) {
            zipFiles.add(destFile.getAbsolutePath());
        }

        // grab file's parent directory structure
        File destinationParent = destFile.getParentFile();

        // create the parent directory structure if needed
        destinationParent.mkdirs();

        try {
            // extract file if not a directory
            if (!entry.isDirectory()) {
                BufferedInputStream is =
                        new BufferedInputStream(zipFile.getInputStream(entry));
                int currentByte;
                // establish buffer for writing file
                byte data[] = new byte[BUFFER];

                // write the current file to disk
                FileOutputStream fos = new FileOutputStream(destFile);
                BufferedOutputStream dest =
                        new BufferedOutputStream(fos, BUFFER);

                // read and write until last byte is encountered
                while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, currentByte);
                }
                dest.flush();
                dest.close();
                is.close();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
    zipFile.close();

    for (Iterator iter = zipFiles.iterator(); iter.hasNext();) {
        String zipName = (String)iter.next();
        doUnzip(
            zipName,
            destinationDirectory +
                File.separatorChar +
                zipName.substring(0,zipName.lastIndexOf(".zip"))
        );
    }

}
Mardellmarden answered 2/7, 2012 at 5:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.