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

sitijaafarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
what makes you think that?

have you checked the file with something like winzip to see whats in it?
0
sitijaafarAuthor Commented:
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.
0
objectsCommented:
ads some more logging to the code to get a better idea of whats going on
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

anilallewarCommented:
I was able to get this running using the class below and command line invocation

java -Darchive_path=C:\Capture -Dzip_path=C:\Capture\sql.zip com.anil.java.files.ZipFileReader

ZipFileReader.java
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anilallewarCommented:
Here is the zip file

sql.zip
0
anilallewarCommented:
Can you post your zip file? There might be something wrong with the zip file that is causing the issue.
0
CEHJCommented:
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

0
CEHJCommented:
(Of course, if you want to copy any and all .sql files, then don't bail out of the loop)
0
manavilaiCommented:
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.
0
CEHJCommented:
>>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

0
manavilaiCommented:
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.
0
anilallewarCommented:
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.
0
CEHJCommented:
>>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
0
sitijaafarAuthor Commented:
Sorry for the late reply. Thank you for the great feedback.  Finally based on the anilallewar, I find out what wrong with my code.  
0
sitijaafarAuthor Commented:
Perfect
0
sitijaafarAuthor Commented:
Actually the problem is the way I put the code to run the .sql file.
0
CEHJCommented:
sitijaafar, can you tell me why you accepted that answer - it doesn't solve your problem?
0
CEHJCommented:
>>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 ..?
0
sitijaafarAuthor Commented:
Because based on the anilallewar code I figure out the way I put the code to execute the .sql file is not correct.
0
CEHJCommented:
Please post your current code
0
sitijaafarAuthor Commented:
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

0
CEHJCommented:
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
0
sitijaafarAuthor Commented:
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.
0
CEHJCommented:
>>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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.