Creating zip archive
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Svetoslav_Vasilev
Posted On:   Tuesday, October 30, 2001 06:47 AM

Hi, I have some trouble when trying to unzip a zip archive created through my applet.WinZip shows the following error: invalid compressed data to inflate Here is the code segment where i create the zip archive: java.io.File zipFile = new java.io.File("C:\Temp\upload.zip"); FileOutputStream fw = new FileOutputStream(zipFile); ZipOutputStream zStream = new ZipOutputStream(fw); zStream.setMethod(ZipOutputStream.DEFLATED); for(int i=0;i byte[] buffer = new byte[4096]; java.io.File f; f = new java.io.File(m_FilePaths[i]); FileInputStream fr = new FileInputStream(f); ZipEntry entry; if(f.getName().substring(f.g   More>>

Hi,
I have some trouble when trying to unzip a zip archive created through my applet.WinZip shows the following error:
invalid compressed data to inflate

Here is the code segment where i create the zip archive:

			
java.io.File zipFile = new java.io.File("C:\Temp\upload.zip");
FileOutputStream fw = new FileOutputStream(zipFile);
ZipOutputStream zStream = new ZipOutputStream(fw);
zStream.setMethod(ZipOutputStream.DEFLATED);


for(int i=0;i
byte[] buffer = new byte[4096];
java.io.File f;
f = new java.io.File(m_FilePaths[i]);

FileInputStream fr = new FileInputStream(f);
ZipEntry entry;
if(f.getName().substring(f.getName().lastIndexOf(".")).equals(".asp")){
//here the file is renamed if it is an .asp
entry = new ZipEntry(f.getName()+"_");
}
else{
//the file name is kept as it is
entry = new ZipEntry(f.getName());
}

zStream.putNextEntry(entry);
while((limit = fr.read(buffer,0,4096))!=-1){
zStream.write(buffer,0,limit);
}
zStream.closeEntry();
fr.close();
fr = null;
}

zStream.close();
fw.close();
zStream = null;
fw = null;

If there is something that i do wrong please correct it at the code snippet.

Thanks.

   <<Less

Re: Creating zip archive

Posted By:   jehu_fleuriot  
Posted On:   Monday, January 28, 2002 09:11 AM


Hi,
i have modified the code for Creating Zip archive and i have seen where were the error.
I thing that there is a variable named :
m_FilePaths which is not initialized
If someone could help me it will be good.
thanks and here is my modification.



import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipEntry ;



public class zip
{

/**
* the path to the file
//The name of the file to unzip
//as zr.zip
*/
static String path;
static java.io.File zipFile;
static FileOutputStream fw ;
static ZipOutputStream zStream;

/**
* the limit of the buffer
*/
static int limit;

/** Here we give the file to zip
*@exception Exception from the java.io.File,FileOutputStream or ZipOutputStream
*/
public void zip(String p)throws Exception {
this.path = p;
this.zipFile = new java.io.File(path);
this.fw = new FileOutputStream(zipFile);
this.zStream = new ZipOutputStream(fw);
}

/** here we do the core */

public void main(String args[]){

//Virtual Initialisation of the constructor
try {
this.zip(new String(args[0]));

zStream.setMethod(ZipOutputStream.DEFLATED);
//Just to correct a bug for the moment
String[] m_FilePaths = new String[5];
for(int i=0;ibyte[] buffer = new byte[4096];
java.io.File f;
f = new java.io.File(m_FilePaths[i]);

FileInputStream fr = new FileInputStream(f);
ZipEntry entry;
if(f.getName().substring(f.getName().lastIndexOf(".")).equals(".asp")){
file://here the file is renamed if it is an .asp
entry = new ZipEntry(f.getName()+"_");
}
else{
file://the file name is kept as it is
entry = new ZipEntry(f.getName());
}

zStream.putNextEntry(entry);
while((limit = fr.read(buffer,0,4096))!=-1){
zStream.write(buffer,0,limit);
}
zStream.closeEntry();
fr.close();
fr = null;
}

end();

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

/**
* here we help the gabage collector
*
* @exception java.lang.Exception
*/
public void end()throws java.lang.Exception
{
zStream.close();
fw.close();
zStream = null;
fw = null;
}

//End of the class
}
About | Sitemap | Contact