Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why isnt my unzip program working??  RSS feed

 
v ray
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package Trial;
import java.util.zip.*;
import java.io.*;

public class MyExtractor
{
public static void main(String args[])throws Exception
{
FileInputStream fis=new FileInputStream("C:\\Vray\\xml_files\\profiler-common.zip");
ZipInputStream zis=new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while((entry=zis.getNextEntry())!=null)
{
String name=entry.getName();
System.out.println("name="+name);
String outputDir="C:\\Vray\\xml_files\\";
FileOutputStream out=new FileOutputStream(outputDir+File.separator+entry.getName());
BufferedOutputStream dest=new BufferedOutputStream(out,111148);
int count;
byte data[] = new byte[111148];
while ((zis.read(data, 0, 2048)) != -1)
{
dest.write(data, 0, zis.read(data, 0, 2048));

}
}

}
}

Why doesnt this work? a file is created always, but it is empty.damn!
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code is hard to read without any indentation. In the future, please use code tags and indent the code. Thank you.

One problem here is that you didn't close the dest stream. That means it's possible (probable) that some of the data you've written is still in the buffer, not yet written to file, when the program exits.

Another problem is that you always write a block of 2048 bytes at a time. What if there are fewer bytes available? You need to remember the return value for read(), and use it to control how many bytes you write.
[ May 22, 2007: Message edited by: Jim Yingst ]
 
v ray
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm so sorry about the bad indentation, I wasnt aware of the code tag, the code was proper in my eclipse window, but I copied it inside the textarea here, the indentation was gone.
But thank you so much! You were right, I made the 2 changes and it worked! I think the main reason it wasnt working was that i hadnt closed the dest stream.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!