Link to home
Start Free TrialLog in
Avatar of sitijaafar
sitijaafarFlag for Malaysia

asked on

How to unzip the file that contains more than one file

I have a audit.zip file in that file I have two file example A.sql and B.sql.  It's look like not zip the both file, it only zip one file. Below is my code:



String tempdir = System.getProperty("archive_path");
final int BUFFER = 2048;
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(zipfile);
            
CheckedInputStream checksum = new CheckedInputStream(fis, new Adler32());
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(checksum));
           
ZipEntry entry;
						

			
while((entry = zis.getNextEntry()) != null) 
{
            	
            	
if(!entry.toString().endsWith(".sql"))
{
if (log.isDebugEnabled())
log.debug("Not an sql file");
            		
exitVal = "-1"; // not sql file
break;
}
            		
int count;
byte unzipdata[] = new byte[BUFFER];
               
// write the files to the disk
FileOutputStream fos = new FileOutputStream(tempdir + "//" + entry.getName());
dest = new BufferedOutputStream(fos, BUFFER);
               
if(log.isDebugEnabled())
log.debug("Extracting entryname : " +entry.getName());
              
while ((count = zis.read(unzipdata, 0, BUFFER)) != -1) {
dest.write(unzipdata, 0, count);
}
            	
dest.flush();
dest.close();

Open in new window

Avatar of Mick Barry
Mick Barry
Flag of Australia image

what makes you think that?

have you checked the file with something like winzip to see whats in it?
Avatar of sitijaafar

ASKER

Yes, I check when zip it's have two file in the zip file.  Actually after unzip, I'm used that file to create data and insert data.  For exampe the A.sql contains create table statement, and the B.sql for insert data into database.  It can create the table, but cannot insert because it said cannot find the B.sql file.  So, that's means it only unzip one file A.sql.
ads some more logging to the code to get a better idea of whats going on
ASKER CERTIFIED SOLUTION
Avatar of anilallewar
anilallewar
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
Here is the zip file

sql.zip
Can you post your zip file? There might be something wrong with the zip file that is causing the issue.
Your current implementation is to bail as soon as you find a non-sql file. Use the following instead of what you have at the moment (it reflects what you're currently doing) and check your debug log:
 log.debug(String.format("%s is not an sql file. Now stopping searching remainder of archive.", entry.getName()));

Open in new window

(Of course, if you want to copy any and all .sql files, then don't bail out of the loop)
Avatar of manavilai
manavilai

your application unzips perfectly fine.just gave it a try, maybe you are seeing a different issue not related to this code you have posted.
>>your application unzips perfectly fine.

No it doesn't unfortunately - which is why sitijaafar is asking this question. It suffers from the problem i mentioned earlier. See the test output below, after the debug change i mentioned above
goose@amd:/tmp$ jar tf test.zip 
C.sql
C.duf
A.sql
B.sql
B.duf
goose@amd:/tmp$ java  -Darchive_path=/tmp -cp $CP Zip test.zip
14:13:43.052 [main] DEBUG Zip - Extracting entryname : C.sql
14:13:43.070 [main] DEBUG Zip - C.duf is not an sql file. Now stopping searching remainder of archive.
goose@amd:/tmp$ ls -l *.sql
-rw-r--r-- 1 goose goose 3 2010-04-15 14:13 C.sql

Open in new window

CEHJ : You are right,I see your point.

Maybe i just ran it with only .sql file inside the zip file.
as provided in the sample from anil.
I think what @sitijaafar was saying is that the zip file contains both files with .sql extensions; so the prob that @CEHJ is mentioning should not have come in this case.
>>so the prob that @CEHJ is mentioning should not have come in this case.

That looks like something other than the zip file that's really being used. It doesn't contain files with any recognisable sql in it
Sorry for the late reply. Thank you for the great feedback.  Finally based on the anilallewar, I find out what wrong with my code.  
Perfect
Actually the problem is the way I put the code to run the .sql file.
sitijaafar, can you tell me why you accepted that answer - it doesn't solve your problem?
>>Actually the problem is the way I put the code to run the .sql file.

No it isn't. There may be an *additional* problem with how you're running the sql file, but that's not relevant at this point. That still doesn't explain why you accepted that answer ..?
Because based on the anilallewar code I figure out the way I put the code to execute the .sql file is not correct.
Please post your current code
Here is the new code
String fileName[];
fileName = new String[2];
        			
int i=0;
while((entry = zis.getNextEntry()) != null) 
{
       if (log.isDebugEnabled())
               log.debug("Extracting: " +entry);
                    	
       if(!entry.toString().endsWith(".sql"))
       {
            if (log.isDebugEnabled())
                    log.debug("Not an sql file");
                    		
            exitVal = "-1"; // not sql file
            break;
        }
                    		
int count;
byte unzipdata[] = new byte[BUFFER];
                       
// write the files to the disk
FileOutputStream fos = new FileOutputStream(tempdir + "//" + entry.getName());
dest = new BufferedOutputStream(fos, BUFFER);
                       
if(log.isDebugEnabled())
log.debug("Extracting entryname : " +entry.getName());
                    	
fileName[i]=entry.getName();
                      
while ((count = zis.read(unzipdata, 0, BUFFER)) != -1) {
            dest.write(unzipdata, 0, count);
 }
i++;
 dest.flush();
 dest.close();  
}
zis.close();
                    
	if(log.isDebugEnabled()){
            log.debug("fileName1 : " +fileName[0]);
            log.debug("fileName1 : " +fileName[1]);
          }              
            			
				
Process child=null;
String execStr = "";
String create_table = "create_table";
String line = null;				
					
	            	
execStr = "sqlcmd -U" + dbUsername + " -P" + dbPassword + " -d" + dbName + " -S" + dbHost +" -i\""+ tempdir + "\\" + fileName[1]+"\"";	
            		
child = runtime.exec(execStr);
if (log.isDebugEnabled()){
    log.debug("source execstr = "+execStr);			
    					 } 
InputStream stdin = child.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);            	    
            	    
            	    
if (log.isDebugEnabled()){
    log.debug("bcp <OUTPUT> ");			
    					 }
while ( (line = br.readLine()) != null) {
if (log.isDebugEnabled()){
	log.debug(line);			
    	 }
      }            	
if (log.isDebugEnabled()){
    log.debug("bcp <OUTPUT> ");			
    	}
            	    
iWait = child.waitFor();           		
String dataFile = fileName[0];            		
String dataTable = dataFile.substring(0, dataFile.indexOf(".sql"));
execStr = "bcp " + dbName + ".dbo." +dataTable+" in \""+tempdir + "\\" + fileName[0] + "\" -c -S " + dbHost +" -U "+ dbUsername + " -P " + dbPassword;
            		
            		
child = runtime.exec(execStr);
  if (log.isDebugEnabled()){
    	log.debug("source execstr = "+execStr);			
     } 
stdin = child.getInputStream();
isr = new InputStreamReader(stdin);
br = new BufferedReader(isr);            	    
            	                	    
    
while ( (line = br.readLine()) != null) {
if (log.isDebugEnabled()){
    	log.debug(line);			
    }
            	    }            	
	
            	    
iWait = child.waitFor();
            		
if(iWait==0)
{   
String getFileName = fileName[0];     					
   exitVal=getFileName.substring(0, getFileName.indexOf('.'));
}
  else
exitVal="-2";
     					
 if (log.isDebugEnabled())
 {
     log.debug("exitVal : "+exitVal);					
}
     	           
  File f1 = new File (tempdir + "/" + fileName[0]);
  f1.delete();
  File f2 = new File (tempdir + "/" + fileName[1]);
  f2.delete();

if (log.isDebugEnabled())
     log.debug("f1 to delete : "+tempdir + "/" + fileName[0]);
     log.debug("f2 to delete : "+tempdir + "/" + fileName[1]);
                 
                 
if (log.isDebugEnabled())
     log.debug("Checksum:"+checksum.getChecksum().getValue());
  

Open in new window

Your code does not fix the error. If you have an archive that consists of anything other than ALL sql files, it will almost certainly fall over
Maybe it's because of my poor english make you misunderstand, actually my question is how to unzip file if inside the zip file is more than one, actually it's doesn't matter wheter it's .sql file or not.  But your give me an idea that I should a code for make sure the the archive must consist .sql file only.  You're right CEHJ. I never thinking about that.
>>actually it's doesn't matter wheter it's .sql file or not.

Why is your code checking (in the wrong way) that it's .sql then?

There seems to be some confusion here - i suggest you review my comments very carefully and test it with various archives