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

Zip getCompressSize more efficient method ? Please ?  RSS feed

 
Mark Nicholas
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I've got this method to determine the file size if I compress the file, I'm just wondering is there a better way ?
Also note that out.closeEntry(); must be called before you can obtain the size using getCompressSize() otherwise you get -1 ?
public static long getCompressSize(String file)
{
long compsize = 0;
byte[] buf = new byte[1024];
try
{
ZipOutputStream out = new ZipOutputStream(new FileOutputStream("temp.zip"));
FileInputStream in = new FileInputStream(file);
ZipEntry ze = new ZipEntry("dummy");
out.putNextEntry(ze);
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
out.closeEntry();

// need to close the entry before
// you can obtain details
compsize = ze.getCompressedSize();
in.close();
out.close();
}
catch (FileNotFoundException e){}
catch (IOException e){}
return compsize;
}

[ August 20, 2002: Message edited by: Mark Nicholas ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!