-
October 14th, 2014, 02:25 AM
#1
read every entry of a zip file
I'm trying to make a program a program that can choose a zip file and iterate through each file. For now I'm happy with either assigning a string variable as the full path to the file or assigning the file as an object. Later on I want to be able to read and rewrite the files (text files) so I don't know whats more suited.
However I cannot seem to get this working, right now Im trying to print a list of all the files.
Code:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.swing.JFileChooser;
public class Tester {
public static void main(String[] args) throws IOException {
final JFileChooser fc = new JFileChooser();
int response = fc.showOpenDialog(null);
String fileName;
if(response == JFileChooser.APPROVE_OPTION) {
fileName = fc.getSelectedFile().toString();
}else {
fileName = "The file open operation was cancelled";
}
for (int i = 0; i < args.length; i++) {
FileInputStream fin = new FileInputStream(fileName);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
System.out.println("Unzipping " + ze.getName());
FileOutputStream fout = new FileOutputStream(ze.getName());
for (int c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
zin.closeEntry();
fout.close();
}
zin.close();
}
}
}
Any tips on how to first get this to work and then how to best work with text files within a zip file?
Edit: I just realized that the for loop didn't work. The args.length always returned zero. I set this to 10 because I know the zip files Im testing this on won't have more than a few entries. But how do I do this properly?
Last edited by mikeymoo; October 14th, 2014 at 04:01 AM.
Reason: Mistake in code
-
October 14th, 2014, 05:13 PM
#2
Re: read every entry of a zip file
Edit: I just realized that the for loop didn't work. The args.length always returned zero. I set this to 10 because I know the zip files Im testing this on won't have more than a few entries. But how do I do this properly?
Have you tried something like this to get the number of entries in a zip file?
Code:
import java.util.zip.ZipFile;
.
.
.
final ZipFile file = new ZipFile(fileName);
int numberOfEntries = file.size();
System.out.println(numberOfEntries);
file.close();
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|