Link to home
Start Free TrialLog in
Avatar of kls1
kls1

asked on

Sizes of Files and Zipfiles, etc

I am working on an application that, for part of it, a zip file is created. Where the zipfile is placed depends on its size (i.e. if it causes the current directory to hit capacity or not).  

In doing a little poking around, this is what I have found:
- In order to determine the size a zip file will be that holds a given set of files, I actually need to construct it.
- A File object is immutable, so once it is created, I cannot make it 'point' to another directory/filepath.

Based on this, the only thing I can figure is that if the zip file is too large, I will actually need to repeat the process of creating it in order to put it in a secondary directory if the first hits capacity.

I would like to know if others agree with this or if there is another reasonable way to handle this.
Avatar of sciuriware
sciuriware

I would never 'reuse' a File object. Nowadays we worry about Gb, not about some bytes.

If you can not predict the size of the zipfile, it's better to create it in a place
with lots of space and later to copy it.
The zipping process takes a lot of CPU and copying is cheap.

Beware that some files GROW when zipped, e.g. "white noice" files.

;JOOP!
Avatar of kls1

ASKER

The question is...how do you "move" a zip file in Java? Since a File object is immutable....
Avatar of kls1

ASKER

From what I see, and I could be wrong:

You construct a File object with the path where you want your Zip file
Then you construct your zip from from that File object

Is there something I'm missing? So, once I have a zip file constructed, how can I move it?
SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Sample code here...:::

import java.io.File;
import java.util.*;
/***************************************************************** ******************************
* This program finds the actual content size of the file not the size of the file in the disk.
* Eg: Check the 1 Byte text file for its actual content size and the size of that same file in the
* disk.
******************************************************************************* ****************/

public class FindFileSize
{
double fileSizeKB;



public FindFileSize()
{
/**Specify the location and name of the file whose size is to be found **/

File f = new File("C:\\arulTest\\arul2.zip");

String fileLength = String.valueOf(f.length());
int fileLengthDigitCount = fileLength.length();
double fileLengthLong = f.length();
double decimalVal = 0.0;
String howBig = "";

System.out.println("fileLengthDigitCount is..."+fileLengthDigitCount);

if(f.length()>0)
{
if(fileLengthDigitCount < 5)
{
fileSizeKB = Math.abs(fileLengthLong);
howBig = "Byte(s)";
}
else if(fileLengthDigitCount >= 5 && fileLengthDigitCount <=6)
{
fileSizeKB = Math.abs((fileLengthLong/1024));
howBig = "KB";
}
else if(fileLengthDigitCount >= 7 && fileLengthDigitCount <= 9)
{
fileSizeKB = Math.abs(fileLengthLong/(1024*1024));
howBig = "MB";
}
else if(fileLengthDigitCount >9)
{
fileSizeKB = Math.abs((fileLengthLong/(1024*1024*1024)));
decimalVal = fileLengthLong%(1024*1024*1024);
howBig = "GB";
}
}
System.out.println("....bytes....."+fileSizeKB);
String finalResult = getRoundedValue(fileSizeKB);
System.out.println("\n....Final Result....."+finalResult+" "+howBig);
}

private String getRoundedValue(double decimalVal)
{
System.out.println("\nThe first call......."+decimalVal);

long beforeDecimalValue = decimalTokenize(decimalVal,1);
long afterDecimalValue = decimalTokenize(decimalVal,2);
long decimalValueLength = String.valueOf(afterDecimalValue).length();
long dividerVal = divider(decimalValueLength-1);
long dividedValue = afterDecimalValue/dividerVal;
String finalResult=String.valueOf(beforeDecimalValue)+"."+String.valueOf(dividedValue) ;

System.out.println("\nfinalResult......."+finalResult);

return finalResult;
}

private long divider(long argLength)
{
long varDivider=1;

for(int i=0;i<(argLength-1);i++)
{
varDivider=varDivider*10;
}

return varDivider;
}

private long decimalTokenize(double decimalVal,int position)
{

long returnDecimalVal=0;
String strDecimalVal="";

if(decimalVal >0)
strDecimalVal = String.valueOf(decimalVal);

if(strDecimalVal.length()>0)
{
StringTokenizer decimalToken = new StringTokenizer(strDecimalVal,".");
//System.out.print("\n String tokenized successfully"+decimalToken.countTokens());
//int count = decimalToken.countTokens();

if(position==1)
{
returnDecimalVal = Long.parseLong(decimalToken.nextToken());
}
else if(position==2)
{
decimalToken.nextToken();
returnDecimalVal = Long.parseLong(decimalToken.nextToken());
}
}
return returnDecimalVal;
}

/**
*
* @param args
*/
public static void main(String[] args)
{
FindFileSize findFileSize = new FindFileSize();
}
}

R.K
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kls1

ASKER

mightyone: So, your method above doesn't actually write the data to the hard drive until the writeByteArray method? (i.e. createZip only creates the zip in RAM and gets the size of the zip?)
Avatar of kls1

ASKER

mightyone: I see that you are sending in a path to the writeByteArray, so I'm guessing it is actually writing this zip file to the hard drive during this method, correct?
yes...
zip is created in RAM.

the path is to read the files to zip from, see :>>File oneFile = new File(path+filenames[idx]);

first time you write is here:
 out = new FileOutputStream(name);//name should be somehow like "c:\tmp\file.zip"