0

在我的应用程序中,我从服务器下载压缩文件并通过此处的代码提取文件,但压缩文件包含 .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 回答