Java tutorial
/** * Copyright(c) 2014 DRAWNZER.ORG PROJECTS -> ANURAG * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * anurag.dev1512@gmail.com * */ package drawnzer.anurag.archivereaddemo; import java.io.File; import java.util.zip.ZipException; import android.os.Bundle; import android.os.Environment; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; import drawnzer.anurag.archiveread.ReadArchive; import drawnzer.anurag.rarHelper.RarObj; /** * * @author Anurag.... * */ public class RarFragment extends Fragment { //main object holding the archive.... private ReadArchive read_rar; //true if archive is opened.... private boolean opened; private String rar_path = null; //sdcard path.... private String path = Environment.getExternalStorageDirectory().getAbsolutePath(); public RarFragment() { // TODO Auto-generated constructor stub opened = false; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return inflater.inflate(R.layout.ls, container, false); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { // TODO Auto-generated method stub final ListView lsView = (ListView) view.findViewById(R.id.ls); lsView.setSelector(R.drawable.button_click); lsView.setAdapter(new FileAdapter(getActivity(), new File(path).listFiles())); lsView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub if (opened) { //zip file already opened.... //we are selecting sub files inside of zip file.... //getting selected entry from list view.... RarObj obj = (RarObj) lsView.getAdapter().getItem(arg2); //getting path of selected folder inside zip archive.... rar_path = obj.getPath(); if (obj.isFile()) { // a files is selected inside of archive.... Toast.makeText(getActivity(), "A file is selected....", Toast.LENGTH_SHORT).show(); } else { //a folder is selected inside zip archive.... //so further listing the selected folder.... try { lsView.setAdapter(read_rar.getAdapter(rar_path, getActivity())); } catch (ZipException e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(getActivity(), "Something went wrong....", Toast.LENGTH_SHORT).show(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(getActivity(), "Something went wrong....", Toast.LENGTH_SHORT).show(); } } return; } //folder or some file is selected.... //getting the file.... File getFile = (File) lsView.getAdapter().getItem(arg2); if (getFile.isDirectory()) { //updating th path.... path = path + "/" + getFile.getName(); //updating the listview as per new path.... lsView.setAdapter(new FileAdapter(getActivity(), new File(path).listFiles())); } else { //file is selected.... if (getFile.getName().endsWith(".rar")) { //open the zip file.... opened = true; read_rar = new ReadArchive(getFile); if (rar_path == null) rar_path = "/"; try { lsView.setAdapter(read_rar.getAdapter(rar_path, getActivity())); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(getActivity(), "Invalid zip file....", Toast.LENGTH_SHORT).show(); } } else Toast.makeText(getActivity(), "Choose a rar file to open....", Toast.LENGTH_SHORT).show(); } } }); lsView.setOnItemLongClickListener(null); } }