如何在安装应用程序时解压缩zip文件,然后在android中启动我的应用程序

时间:2014-02-27 11:10:51

标签: android

我的资产文件夹中有一个zip文件,我希望在安装时和启动我的应用程序之前解压缩该文件。所以可以在安装过程中解压缩文件。 请求帮我解决这个问题..

我已经在我的启动画面上完成了这项操作并且它正在运行但是想在安装我的应用程序时和启动画面之前这样做。

公共类SplashScreenActivity扩展了Activity {

private ProgressDialog mProgressDialog;
private Context context;
AssetManager assetManager;
InputStream is;

String Url = "http://remote.careerfinity.com/User/UI.zip";
String unzipLocation = Environment.getExternalStorageDirectory()
        + "/unzipFolder/";
String StorezipFileLocation = Environment.getExternalStorageDirectory()
        + "/DownloadedZip";
String DirectoryName = Environment.getExternalStorageDirectory()
        + "/unzipFolder/files/";



@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    //assetManager = getApplicationContext().getAssets().open("Device-Initial-UI-Files.zip");

    Thread timer = new Thread() {

        public void run() {
            try {
                sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                Intent gotoStart = new Intent(getApplicationContext(),MainActivity.class);
                startActivity(gotoStart);
            }
        }
    };
    timer.start();  

}



public void unzip() throws IOException {
    mProgressDialog = new ProgressDialog(SplashScreenActivity.this);
    mProgressDialog.setMessage("Please Wait...");
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    mProgressDialog.setCancelable(false);
    mProgressDialog.show();
    new UnZipTask().execute(StorezipFileLocation, DirectoryName);
}

public class UnZipTask extends AsyncTask<String, Void, Boolean> {
    @SuppressWarnings("rawtypes")
    @Override
    protected Boolean doInBackground(String... params) {
        String filePath = params[0];
        String destinationPath = params[1];

        File archive = new File(filePath);
        try {
            ZipFile zipfile = new ZipFile(archive);
            for (Enumeration e = zipfile.entries(); e.hasMoreElements();) {
                ZipEntry entry = (ZipEntry) e.nextElement();
                unzipEntry(zipfile, entry, destinationPath);
            }

            Ziputil d = new Ziputil(StorezipFileLocation, DirectoryName);
            d.unzip();

        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        mProgressDialog.dismiss();
    }

    private void unzipEntry(ZipFile zipfile, ZipEntry entry,
            String outputDir) throws IOException {

        if (entry.isDirectory()) {
            createDir(new File(outputDir, entry.getName()));
            return;
        }

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

        // Log.v("", "Extracting: " + entry);
        BufferedInputStream inputStream = new BufferedInputStream(
                zipfile.getInputStream(entry));
        BufferedOutputStream outputStream = new BufferedOutputStream(
                new FileOutputStream(outputFile));

        try {

        } finally {
            outputStream.flush();
            outputStream.close();
            inputStream.close();
        }
    }

    private void createDir(File dir) {
        if (dir.exists()) {
            return;
        }
        if (!dir.mkdirs()) {
            throw new RuntimeException("Can not create dir " + dir);
        }
    }
}





private void recursiveDelete(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory())
        for (File child : fileOrDirectory.listFiles())
            recursiveDelete(child);

    fileOrDirectory.delete();
}

}

1 个答案:

答案 0 :(得分:0)

使用以下课程: -

public class Decompress
{
private Context mContext;
private Logger mLogger;

public Decompress(Context context)
{
    this.mContext = context;
    mLogger = Logger.getInstance(mContext);
}

/**
 * Unzips given zip file in specified location.
 * 
 * @param zipFile
 * <br>
 *            Passing zip file path - will search file in File directory. <br>
 *            Passing zip file name (without extension)- will search in apps
 *            raw folder.<br>
 * 
 * @param unZipLocation
 * <br>
 *            Pass path-location where 'zipFile' will be unzipped.
 */
@SuppressWarnings("resource")
public void unZip(String zipFile, String unZipLocation)
{
    try
    {
        System.out.println(">>> Start Unzip..." + Calendar.getInstance().getTime());

        InputStream fin = null;
        try
        {
            fin = new FileInputStream(zipFile);
        }
        catch(Exception e)
        {
            int resId = mContext.getApplicationContext().getResources().getIdentifier(zipFile, "raw", mContext.getApplicationContext().getPackageName());
            fin = mContext.getResources().openRawResource(resId);
        }

        ZipInputStream zin = new ZipInputStream(fin);
        ZipEntry ze = null;
        int count;

        while((ze = zin.getNextEntry()) != null)
        {
            Log.v("Decompress", "Unzipping " + ze.getName());

            if(ze.isDirectory())
            {
                dirChecker(ze.getName(), unZipLocation);
            }
            else
            {
                FileOutputStream fout = new FileOutputStream(unZipLocation + ze.getName());
                byte data[] = new byte[128 * 1024];

                while((count = zin.read(data)) != -1)
                {
                    fout.write(data, 0, count);
                }
                data = null;
                zin.closeEntry();
                fout.close();
            }
        }
        fin.close();
        zin.close();
        System.out.println(">>> Unzip completed..." + Calendar.getInstance().getTime());
    }
    catch(Exception e)
    {
        mLogger.error(Logger.ERROR_CODE_API, e);
    }
}

private void dirChecker(String dir, String unZipLocation)
{
    File f = new File(unZipLocation + dir);

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