0

私のアプリケーションでは、サーバーから zip 形式でダウンロードし、コードを使用してファイルを抽出していますが、zip ファイルには .DS_store ファイルが含まれているため、解凍に失敗します。それを回避する方法はありますか。

/*/////

                public class Decompress { 
                      private String _zipFile; 
                      private String _location; 

                      public Decompress(String zipFile, String location) { 
                        _zipFile = zipFile; 
                        _location = location; 

                        _dirChecker(""); 
                      } 

                      public boolean unZip() { 
                        try  { 
                          FileInputStream fin = new FileInputStream(_zipFile); 
                          ZipInputStream zin = new ZipInputStream(fin); 
                          ZipEntry ze = null; 
                          while ((ze = zin.getNextEntry()) != null) { 
                            Log.v("Decompress", "Unzipping " + ze.getName()); 

                            if(ze.isDirectory()) { 
                              _dirChecker(ze.getName()); 
                            } else { 
                                 byte[] buffer = new byte[1024];

                              FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
                //            for (int c = zin.read(); c != -1; c = zin.read(buffer,0,1024)) { 
                //              fout.write(buffer,0,c); 
                //            } 
                              int c;
                              while (( c = zin.read(buffer,0,1024)) >= 0) {
                                  fout.write(buffer,0,c); 
                                }

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

                          } 
                          zin.close(); 
                        } catch(Exception e) { 
                          Log.e("Decompress", "unzip", e); 
                          return false;
                        } 
                           return true;
                      } 

                      private void _dirChecker(String dir) { 
                        File f = new File(_location + dir); 

                        if(!f.isDirectory()) { 
                          f.mkdirs(); 
                        } 
                      } 


                    } 

//// */

4

1 に答える 1

1

このコードを使用してください

public class ZipHelper
{
    boolean zipError=false;

    public boolean isZipError() {
        return zipError;
    }

    public void setZipError(boolean zipError) {
        this.zipError = zipError;
    }

    public boolean unzip(String archive, File outputDir)
    {
        try {
            Log.d("control","ZipHelper.unzip() - File: " + archive);
            ZipFile zipfile = new ZipFile(archive);
            for (Enumeration<? extends ZipEntry> e = zipfile.entries(); e.hasMoreElements(); ) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                unzipEntry(zipfile, entry, outputDir);

            }
            return true;
        }
        catch (Exception e) {
            Log.d("control","ZipHelper.unzip() - Error extracting file " + archive+": "+ e);
            setZipError(true);
            return false;
        }
    }

    private void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException
    {
        if (entry.isDirectory()) {
            createDirectory(new File(outputDir, entry.getName()));
            return;
        }

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

        Log.d("control","ZipHelper.unzipEntry() - Extracting: " + entry);
        BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));

        try {
            IOUtils.copy(inputStream, outputStream);
        }
        catch (Exception e) {
            Log.d("control","ZipHelper.unzipEntry() - Error: " + e);
            setZipError(true);
        }
        finally {
            outputStream.close();
            inputStream.close();
        }
    }

    private void createDirectory(File dir)
    {
        Log.d("control","ZipHelper.createDir() - Creating directory: "+dir.getName());
        if (!dir.exists()){
            if(!dir.mkdirs()) throw new RuntimeException("Can't create directory "+dir);
        }
        else Log.d("control","ZipHelper.createDir() - Exists directory: "+dir.getName());
    }
}

このクラスを次のように呼び出します

 final String exportDirectory = Environment.getExternalStorageDirectory().getAbsolutePath()
                +"/destination file/name/";

        final String exportDirectoryArchivefile = Environment.getExternalStorageDirectory().getAbsolutePath()
                +"/original/file name/"+name;
                final File exportDirectoryFilepath = new File(exportDirectory);
                exportDirectoryFilepath.mkdirs();
                final File exportDirectoryFilepathArchive = new File(exportDirectoryArchivefile);
                boolean flag_unzip =zh.unzip(exportDirectoryArchivefile, exportDirectoryFilepath);
于 2013-02-23T04:12:06.600 に答える