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.
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.
ASKER
The question is...how do you "move" a zip file in Java? Since a File object is immutable....
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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("fileLe ngthDigitC ount is..."+fileLengthDigitCoun t);
if(f.length()>0)
{
if(fileLengthDigitCount < 5)
{
fileSizeKB = Math.abs(fileLengthLong);
howBig = "Byte(s)";
}
else if(fileLengthDigitCount >= 5 && fileLengthDigitCount <=6)
{
fileSizeKB = Math.abs((fileLengthLong/1 024));
howBig = "KB";
}
else if(fileLengthDigitCount >= 7 && fileLengthDigitCount <= 9)
{
fileSizeKB = Math.abs(fileLengthLong/(1 024*1024)) ;
howBig = "MB";
}
else if(fileLengthDigitCount >9)
{
fileSizeKB = Math.abs((fileLengthLong/( 1024*1024* 1024)));
decimalVal = fileLengthLong%(1024*1024* 1024);
howBig = "GB";
}
}
System.out.println("....by tes....."+ 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(afterDecima lValue).le ngth();
long dividerVal = divider(decimalValueLength -1);
long dividedValue = afterDecimalValue/dividerV al;
String finalResult=String.valueOf (beforeDec imalValue) +"."+Strin g.valueOf( dividedVal ue) ;
System.out.println("\nfina lResult... ...."+fina lResult);
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(strDecimal Val,".");
//System.out.print("\n String tokenized successfully"+decimalToken .countToke ns());
//int count = decimalToken.countTokens() ;
if(position==1)
{
returnDecimalVal = Long.parseLong(decimalToke n.nextToke n());
}
else if(position==2)
{
decimalToken.nextToken();
returnDecimalVal = Long.parseLong(decimalToke n.nextToke n());
}
}
return returnDecimalVal;
}
/**
*
* @param args
*/
public static void main(String[] args)
{
FindFileSize findFileSize = new FindFileSize();
}
}
R.K
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.
String fileLength = String.valueOf(f.length())
int fileLengthDigitCount = fileLength.length();
double fileLengthLong = f.length();
double decimalVal = 0.0;
String howBig = "";
System.out.println("fileLe
if(f.length()>0)
{
if(fileLengthDigitCount < 5)
{
fileSizeKB = Math.abs(fileLengthLong);
howBig = "Byte(s)";
}
else if(fileLengthDigitCount >= 5 && fileLengthDigitCount <=6)
{
fileSizeKB = Math.abs((fileLengthLong/1
howBig = "KB";
}
else if(fileLengthDigitCount >= 7 && fileLengthDigitCount <= 9)
{
fileSizeKB = Math.abs(fileLengthLong/(1
howBig = "MB";
}
else if(fileLengthDigitCount >9)
{
fileSizeKB = Math.abs((fileLengthLong/(
decimalVal = fileLengthLong%(1024*1024*
howBig = "GB";
}
}
System.out.println("....by
String finalResult = getRoundedValue(fileSizeKB
System.out.println("\n....
}
private String getRoundedValue(double decimalVal)
{
System.out.println("\nThe first call......."+decimalVal);
long beforeDecimalValue = decimalTokenize(decimalVal
long afterDecimalValue = decimalTokenize(decimalVal
long decimalValueLength = String.valueOf(afterDecima
long dividerVal = divider(decimalValueLength
long dividedValue = afterDecimalValue/dividerV
String finalResult=String.valueOf
System.out.println("\nfina
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()>
{
StringTokenizer decimalToken = new StringTokenizer(strDecimal
//System.out.print("\n String tokenized successfully"+decimalToken
//int count = decimalToken.countTokens()
if(position==1)
{
returnDecimalVal = Long.parseLong(decimalToke
}
else if(position==2)
{
decimalToken.nextToken();
returnDecimalVal = Long.parseLong(decimalToke
}
}
return returnDecimalVal;
}
/**
*
* @param args
*/
public static void main(String[] args)
{
FindFileSize findFileSize = new FindFileSize();
}
}
R.K
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?)
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);//n ame should be somehow like "c:\tmp\file.zip"
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);//n
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!