3

Hi have create from window 7 and put that in server.Now i am downloading the file from server into my SD card.but when i start to unzip it show the error,

 java.util.zip.ZipException: EOCD not found; not a Zip archive?

the code i have use for unzip is

private void unzipEntry(ZipFile zipfile, ZipEntry entry,
                String outputDir) throws IOException {


            if (entry.isDirectory()) {
                createDir(new File(outputDir, entry.getName()));
                return;
            }

            File outputFile = new File(outputDir, entry.getName());
            if (!outputFile.getParentFile().exists()) {
                createDir(outputFile.getParentFile());
            }

            log("Extracting: " + entry);
            BufferedInputStream inputStream = new BufferedInputStream(
                    zipfile.getInputStream(entry));
            BufferedOutputStream outputStream = new BufferedOutputStream(
                    new FileOutputStream(outputFile));

            try {
                IOUtils.copy(inputStream, outputStream);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                outputStream.close();
                inputStream.close();
            }
        }

But when i directly import the file into ddms it work file.

Can anyone know how to resolve the issue then please help me out.

Thank you.

1
  • Hi,i found out that my download was not working perfect, thank you for support
    – Sameer Z.
    Sep 20, 2011 at 12:48

3 Answers 3

2
   FileInputStream fin = new FileInputStream(
                zipfile);
        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;
        while ((ze = zin.getNextEntry()) != null) {

            FileOutputStream fout = new FileOutputStream(
                    unzippath
                            + ze.getName());
            for (int c = zin.read(); c != -1; c = zin.read()) {
                fout.write(c);
            }

            zin.closeEntry();
            fout.close();

        }
        zin.close();

Try this .. This will solve your problem i guess.

4
  • hi,i found out that while ((ze = zin.getNextEntry()) != null) .. return null so not going in while loop can please tell what wrong with code
    – Sameer Z.
    Sep 19, 2011 at 6:40
  • make sure that "zipfile" pointing to proper path containing the zip file.Use log to trace it..
    – Nemo
    Sep 19, 2011 at 9:00
  • can please give the code i using the from SD card,File file = new File(Environment.getExternalStorageDirectory() // + "/images/"+DownloadService.IMAGE_ZIP);
    – Sameer Z.
    Sep 19, 2011 at 9:23
  • Hi @Nemo, Why write FOR statement? I checked your code. It worked correctly without for loop. Sep 2, 2012 at 9:43
1

Check the ZipInputStream example given in the doc here.

0
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipUtil {

  public static void unzip(File archive, File path) throws IOException{
    ZipInputStream zip = null;
    String fileName = null;
    try{
      if(!path.exists()){
        path.mkdirs();
      }
      zip = new ZipInputStream(new FileInputStream(archive));
      ZipEntry zipEntry; 
      while((zipEntry=zip.getNextEntry()) != null) { 
        fileName = zipEntry.getName();
        final File outputFile = new File(path, fileName); 
        writeToStream(new BufferedInputStream(zip), new FileOutputStream(outputFile), false);
        zip.closeEntry();
      }
      zip.close();
      zip = null;
    }finally{
      if(zip != null){
        try{ zip.close(); } catch(Exception e){}
      }
    }
  }
  public static void writeToStream(InputStream in , OutputStream out, boolean closeOnExit) throws IOException 
  {
    byte[] bytes = new byte[2048];
    for (int c = in.read(bytes); c != -1; c = in.read(bytes)) {
      out.write(bytes,0, c);
    }
    if(closeOnExit){
      in.close();
      out.close();
    }
  }
  public static String writeToString(InputStream stream) throws java.io.IOException{
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream,"utf-8"));
        char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.