Mac JAVA Unzipping XLSX

davidlt

macrumors member
Original poster
May 22, 2007
56
0
Lithuania
Hello,

I am trying to unzip XLSX file (yes, Excel) and to get to the data in XML files. The problems is that I cannot extract it.

Here it is:
Code:
david-mac:oracle david$ java Unzip data.xlsx 
Extracting file: [Content_Types].xml
Extracting file: _rels/.rels
Unhandled exception:
java.io.FileNotFoundException: _rels/.rels (No such file or directory)
        at java.io.FileOutputStream.open(Native Method)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
        at java.io.FileOutputStream.<init>(FileOutputStream.java:70)
        at Unzip.main(Unzip.java:123)
If I launch that in Windows I get Access is Denied (message of Exception).

And here is example code I am using:
import java.io.*;
import java.util.*;
import java.util.zip.*;


public class Unzip {

public static final void copyInputStream(InputStream in, OutputStream out)
throws IOException
{
byte[] buffer = new byte[1024];
int len;

while((len = in.read(buffer)) >= 0)
out.write(buffer, 0, len);

in.close();
out.close();
}

public static final void main(String[] args) {
Enumeration entries;
ZipFile zipFile;

if(args.length != 1) {
System.err.println("Usage: Unzip zipfile");
return;
}

try {
zipFile = new ZipFile(args[0]);

entries = zipFile.entries();

while(entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry)entries.nextElement();

if(entry.isDirectory()) {
// Assume directories are stored parents first then children.
System.err.println("Extracting directory: " + entry.getName());
// This is not robust, just for demonstration purposes.
(new File(entry.getName())).mkdirs();
continue;
}

System.err.println("Extracting file: " + entry.getName());
copyInputStream(zipFile.getInputStream(entry),
new BufferedOutputStream(new FileOutputStream(entry.getName())));
}

zipFile.close();
} catch (IOException ioe) {
System.err.println("Unhandled exception:");
ioe.printStackTrace();
return;
}
}

}
 

SydneyDev

macrumors 6502
Sep 15, 2008
346
0
Perhaps if you also called mkdirs on the file entries. Based on the output you pasted, there was no _rels dir entry encountered before the file in the _rels dir.
 

chown33

Moderator
Staff member
Aug 9, 2009
8,664
4,936
vertical
If you just want to unzip the file, use the 'unzip' command:

http://developer.apple.com/mac/library/DOCUMENTATION/Darwin/Reference/ManPages/man1/unzip.1.html


The 'jar' command can also unzip it:
Code:
jar xf path/to/file.xlsx
Or list the contents:
Code:
jar tf path/to/file.xlsx

Finally, this is a faulty assumption:

// Assume directories are stored parents first then children.
There is no requirement to store any entry for dirs. It is perfectly legal to just use /-delimited pathnames in the zip archive. Read the spec.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.