aspose file tools*
The moose likes Java in General and the fly likes creating a zip file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "creating a zip file" Watch "creating a zip file" New topic
Author

creating a zip file

shelly kalra
Greenhorn

Joined: Aug 19, 2003
Posts: 20
I m creating a zip file with the following code
extract_temp is the folder, which has the data to be zipped to story.The extract_ temp has the following structure.
extract_temp
jpg
hello.jpg
hello1.jpg
.....
wav
hello.wav
hello1.wav
.....
story.xml

FileOutputStream FOS = new FileOutputStream(story);// story is the zip file to be created
ZipOutputStream ZOS = new ZipOutputStream(FOS);
String rootPath = "extract_temp";//the folder where i have data to be zipped
File root = new File(rootPath);
recursivelyGetAllFiles(root); //gets all the files in output vector
for(int i = 0;i<output.size(); i++)
{
File x = new File((String)output.elementAt(i));
if(x.isFile())
{
String path = x.getAbsolutePath();
int index = path.indexOf(rootPath);
index += rootPath.length() + 1;
if(index > path.length())
continue;
String name = path.substring(index);

ZipEntry entry = new ZipEntry(name);
ZOS.putNextEntry(entry);
myFileStreamReader = new FileInputStream(path);
sizeRead = 0;
totalSize = 0;
while ((sizeRead = myFileStreamReader.read(readBytes)) > 0)
{
ZOS.write(readBytes, 0, sizeRead);
totalSize += sizeRead;
}
myFileStreamReader = null;
ZOS.flush();
ZOS.closeEntry();
}

}
ZOS.close();

}
catch(Exception e)
{
e.printStackTrace();
}

}
The story that is created when extracted through my java code, could not find the content such as story.xml.
However if extract_temp is manually zipped, the zipped file works absolutely fine.So i m sure that my extract zip code is working properly.
The problem i suspect, is somewhere..while making zip entry in teh above code.
help,
Thanks in anticipation,
Shelly.
Karthikeyan Rajendraprasad
Ranch Hand

Joined: Apr 16, 2003
Posts: 70
this code should help you


Karthikeyan<br />SCJP 1.4, SCWCD.
shelly kalra
Greenhorn

Joined: Aug 19, 2003
Posts: 20
Thanks for your reply.
But your code is same as my code..just that i didnt post the complete code(recursivelyGetAllFiles and few declarations)...my problem still exist.
Thanks
Karthikeyan Rajendraprasad
Ranch Hand

Joined: Apr 16, 2003
Posts: 70
the code i sent you worked well and it can recurse any no of subdirectories. check the security settings of your directories for permission type of problems.
or else post the entire code (zipping and unzipping part). may be i can check ur code.
shelly kalra
Greenhorn

Joined: Aug 19, 2003
Posts: 20
hi,
the code for extacting the zip file
//where basepath is "extract_temp" where to be extracted. and filename is
//story to be extracted that we made by earlier posted code.
public static boolean extractZipFile(String basePath,String fileName)
{
try
{
ZipEntry entry = null;
long size = 0;
InputStream in = new BufferedInputStream(
new FileInputStream(fileName));

ZipInputStream zipFile = new ZipInputStream(in);
File directory = new File(basePath);
if(directory.exists())
{
deleteDirectory(directory);
}
if(!directory.mkdir())
{
System.out.println("Could not create directory");
}
File jpg = new File(FileUtils.getJpgPath());
if(!jpg.exists())
jpg.mkdir();
File wav = new File(FileUtils.getWavPath());
if(!wav.exists())
wav.mkdir();
do {
byte [] data = null;
try {
entry = zipFile.getNextEntry();
} catch (IOException e) {
entry = null;
e.printStackTrace();
}
if(entry == null)
continue;
if (entry != null) {
size = entry.getSize();
}
if(entry.isDirectory())
{
File temp = new File(directory, entry.getName());
temp.mkdir();
continue;
}
//loadResource defined below
data = loadResource( zipFile, size );
if(data!=null)
{
FileOutputStream fos = new FileOutputStream(new File(directory, entry.getName()));
fos.write(data);
fos.close();
}

} while (entry != null);
zipFile.close();
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
private static byte[] loadResource( InputStream in, long size )
{
boolean ok = ((size > 0) && (size < Integer.MAX_VALUE));
byte resource[] = null;
if (ok) {
resource = new byte[(int)size];
ok = ((resource != null) && (resource.length == size));
}
if (ok) {
try {
int done = 0;
int left = (int)size;
while (left > 0) {
int amount = in.read( resource, done, left );
done += amount;
left -= amount;
}
} catch (Exception e) {
ok = false;
}
}
return resource;
}
on unzipping,when i try to get the content.,it crashes with the exception that "story.xml" not found.
My problem is that when i manually cretae zip file from extract_temp, the code works perfectly okay with no exception.
hope that my description is not baffling.
Thanks for all ur favours,
Shelly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: creating a zip file