Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

While unzipping, ZipEntry returns foldername/filename instead of only fodername/ , and these throws File Not Found Exception

General Tech Bugs & Fixes
Max. 2000 characters
Replies

usr_profile.png
Shalini Jaiswal

User

( 7 months ago )

 

I'm creating zip from back-end using PHP. and I want to unzip at Android side.

My Folder structure of Zip includes

  • Folder 1
    • file
    • file
  • Folder 2
    • file
    • file
  • File

To create zip I use following code

foreach ($files as $name => $file)
                    {
                        // echo $file.'  --   ';
                        if (!$file->isDir())
                        {
                            $filePath = $file->getRealPath();
                            $relativePath = substr($filePath, strlen($rootPath) + 1);
                            //echo dirname($file);
                            $zip->addFile($filePath, $relativePath);

                        }
                    }

In android I use following code to unzip

        InputStream is;
        ZipInputStream zis;
        try {
            String filename;
            is = new FileInputStream(zipFile);
            zis = new ZipInputStream(new BufferedInputStream(is));
            ZipEntry ze;
            byte[] buffer = new byte[8192];
            int count;
            while ((ze = zis.getNextEntry()) != null) {
                filename = ze.getName();
                // it will show 
                // it will generate an Exception...
                if (ze.isDirectory()) {
                    File fmd = new File(location + filename);
                    fmd.mkdirs();
                    continue;
                }
                FileOutputStream fout = new FileOutputStream(location + filename);
                while ((count = zis.read(buffer)) != -1) {
                    fout.write(buffer, 0, count);
                }
                fout.close();
                zis.closeEntry();
            }
            zis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

But it's not getting me proper output which I needs.

I expect output at

ze.getName() is FolderName/

but I the actual output is

FolderName/filename

so it do not consider forward slash as directory and it throws File Not Found Exception.

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif