安卓解压zip文件,解压后多级目录显示

    xiaoxiao2021-03-25  100

         因为最近我们的项目当中需要加入zip的解压,因为我们的项目本省就很大了,所以为了节省资源,我决定用ziputils,全部是代码不需要导入jar-包。

        

         我整体的思路是这样,首先解压,解压需要传入一个,解压文件路径,和解压完后才能之后的路径,解压完成之后需要直接跳到解压完成的根目录下,显示出来,并且可以继续点击,可查看文件可以查看,大体是这个效果。看下面的图组。

        

         这个是我们的项目中的所以会有这个云盘模块,从控制台发送文件到客户端,点击.zip文件下载到指定目录下面。

         下面代码是解压的逻辑,就一句话标红色的是。

       

    protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initPermission(); path = Environment.getExternalStorageDirectory().getAbsolutePath(); zipPath = path + "/android.zip"; destPath = path+ "/OutPath/"; zipUtil = new ZipUtil(); new Thread(){ @Override public void run() { super.run(); try { zipUtil.Ectract(zipPath,destPath); } catch (Exception e) { e.printStackTrace(); } } }.start(); }

       下载完成之后直接跳到指定目录下。

                                                              

           这个是直接写了一个activity,然后跳转过去直接intent.pitStringExar(path)传入进去一个路径。

          

    //打开文件 Intent zipIntent = new Intent(mActivity, ZipActivity.class); zipIntent.putExtra("zipfile", newOutPath);//newPath是解压后的路径 startActivity(zipIntent); 上面四张图片就是一级一级的点击打开,最后只打开的txt文档。并且左上角是一个返回键,只需要加上任务栈记住即可。以下是ZipActivity的代码 public class ZipActivity extends RootActivity { private ListView mListviewZip; private ImageView mIvback; private LocalFileAdapter mZipAdapter; public static String ZIPFILE = "zipfile"; private List<File> mZipFileList; private String zipFilePath; private DocManager mDocManager; // path的堆栈 private static Stack<String> mFilePathStack; private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zip); initZipFile(); initViews(); } /** * 初始化zipfile的数据 */ private void initZipFile() { if (mZipFileList == null) { mZipFileList = new ArrayList<>(); } zipFilePath = getIntent().getStringExtra(ZIPFILE); addPath(zipFilePath); } private void initViews() { mDocManager = DocManager.getInstance(this); mIvback = (ImageView) findViewById(R.id.iv_back); mIvback.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (getLastPath().equals(zipFilePath)) { ToastManager.show(ZipActivity.this, R.string.file_up_root); return; } removeLastPath();//移除最上层任务栈 searchViewData(getLastPath());//获取现在最底层的路径 } }); mListviewZip = (ListView) findViewById(R.id.list_zip); mZipFileList = FileUtil.getSortedFiles(zipFilePath); mZipAdapter = new LocalFileAdapter(this, mZipFileList); mListviewZip.setAdapter(mZipAdapter); mListviewZip.setOnItemClickListener(new ListView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String secondPath = mZipFileList.get(position).getAbsolutePath(); File secondFile = new File(secondPath); if(secondFile.isFile()) { openTheFile(secondPath); }else if(secondFile.isDirectory()){ searchViewData(secondPath); addPath(secondPath); } } }); } private void openTheFile(String secondPath) { final Intent intent = mDocManager.getIntentForFile(secondPath); GdLog.i("the file intent is= " + intent); if (intent != null) { mHandler.post(() -> { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); }); }else { mHandler.post(() -> { ToastManager.show(ZipActivity.this,"此文件类型不能被打开"); }); } } /** * 查询view的数据 */ public void searchViewData(final String path) { mZipFileList = FileUtil.getSortedFiles(path); if (mZipFileList.size() > 0) { mZipAdapter.setFiles(mZipFileList); mZipAdapter.setSelectedPosition(-1); mZipAdapter.notifyDataSetChanged(); } } /** * 添加路径到堆栈 * * @param path */ public void addPath(String path) { if (mFilePathStack == null) { mFilePathStack = new Stack<String>(); } mFilePathStack.add(path); } /** * 获取堆栈最上层的路径 * * @return */ public String getLastPath() { return mFilePathStack.lastElement(); } /** * 移除堆栈最上层路径 */ public void removeLastPath() { mFilePathStack.remove(getLastPath()); } } 下面是ZipUtils的代码: public class ZipUtil { /** * 解压缩 * * @param sZipPathFile 要解压的文件 * @param sDestPath 解压到某文件夹 * @return */ @SuppressWarnings("unchecked") public static ArrayList Ectract(String sZipPathFile, String sDestPath) { ArrayList<String> allFileName = new ArrayList<String>(); try { // 先指定压缩档的位置和档名,建立FileInputStream对象 FileInputStream fins = new FileInputStream(sZipPathFile); // 将fins传入ZipInputStream中 ZipInputStream zins = new ZipInputStream(fins); ZipEntry ze = null; byte[] ch = new byte[256]; while ((ze = zins.getNextEntry()) != null) { File zfile = new File(sDestPath + ze.getName()); File fpath = new File(zfile.getParentFile().getPath()); if (ze.isDirectory()) { if (!zfile.exists()) zfile.mkdirs(); zins.closeEntry(); } else { if (!fpath.exists()) fpath.mkdirs(); FileOutputStream fouts = new FileOutputStream(zfile); int i; allFileName.add(zfile.getAbsolutePath()); while ((i = zins.read(ch)) != -1) fouts.write(ch, 0, i); zins.closeEntry(); fouts.close(); } } fins.close(); zins.close(); } catch (Exception e) { System.err.println("Extract error:" + e.getMessage()); } return allFileName; }} 最后是一个adapter和xml文件的代码了,那个我就不传了,因为没有单独写demo,直接写到了公司的项目中,再传的话看起来也比较乱,以上代码还没有做查询数据是的子线程优化等等,希望朋友们看到代码给我指点再就是我还在寻找,解压rar文件,再就是打开ppt格式文件,还有就是cad制图,懂这方面的大神看到的联系我。

        

    转载请注明原文地址: https://ju.6miu.com/read-13822.html

    最新回复(0)