如何在安装应用程序时以及在android中启动应用程序之前解压缩zip文件
我的资产文件夹中有一个zip文件,我希望在安装时和启动应用程序之前解压缩该文件。因此,是否可以在安装期间解压缩文件。。 请帮我解决这个问题 我已经在我的启动屏幕上完成了这项工作,它正在工作,但我希望在安装应用程序时和启动屏幕之前完成这项工作 公共类活动扩展了活动{如何在安装应用程序时以及在android中启动应用程序之前解压缩zip文件,android,Android,我的资产文件夹中有一个zip文件,我希望在安装时和启动应用程序之前解压缩该文件。因此,是否可以在安装期间解压缩文件。。 请帮我解决这个问题 我已经在我的启动屏幕上完成了这项工作,它正在工作,但我希望在安装应用程序时和启动屏幕之前完成这项工作 公共类活动扩展了活动{ private ProgressDialog mProgressDialog; private Context context; AssetManager assetManager; InputStream is; String U
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();
}
private ProgressDialog mProgressDialog;
私人语境;
资产管理人资产管理人;
输入流为;
字符串Url=”http://remote.careerfinity.com/User/UI.zip";
字符串unzipLocation=Environment.getExternalStorageDirectory()
+“/unzipFolder/”;
字符串StorezipFileLocation=Environment.getExternalStorageDirectory()
+“/DownloadedZip”;
String DirectoryName=Environment.getExternalStorageDirectory()
+“/unzipFolder/files/”;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
//assetManager=getApplicationContext().getAssets().open(“设备初始UI文件.zip”);
线程计时器=新线程(){
公开募捐{
试一试{
睡眠(3000);
}捕捉(中断异常e){
e、 printStackTrace();
}最后{
Intent gotoStart=新Intent(getApplicationContext(),MainActivity.class);
星触觉(gotoStart);
}
}
};
timer.start();
}
public void unzip()引发IOException{
mProgressDialog=新建ProgressDialog(SplashScreenActivity.this);
设置消息(“请稍候…”);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE\u微调器);
mProgressDialog.setCancelable(假);
mProgressDialog.show();
新建UnZipTask().execute(StorezipFileLocation,DirectoryName);
}
公共类UnZipTask扩展异步任务{
@抑制警告(“原始类型”)
@凌驾
受保护的布尔doInBackground(字符串…参数){
字符串filePath=params[0];
字符串destinationPath=params[1];
文件存档=新文件(文件路径);
试一试{
ZipFile ZipFile=新ZipFile(存档);
对于(枚举e=zipfile.entries();e.hasMoreElements();){
ZipEntry条目=(ZipEntry)e.nextElement();
取消ZipPentry(zipfile、entry、destinationPath);
}
Ziputil d=新Ziputil(存储ZipFileLocation,目录名);
d、 解压缩();
}捕获(例外e){
返回false;
}
返回true;
}
@凌驾
受保护的void onPostExecute(布尔结果){
mProgressDialog.disclose();
}
私密无效(ZipFile ZipFile、ZipPentry入口、,
字符串outputDir)引发IOException{
if(entry.isDirectory()){
createDir(新文件(outputDir,entry.getName());
回来
}
File outputFile=新文件(outputDir,entry.getName());
如果(!outputFile.getParentFile().exists()){
createDir(outputFile.getParentFile());
}
//Log.v(“,”提取:“+条目);
BufferedInputStream inputStream=新的BufferedInputStream(
getInputStream(条目));
BufferedOutputStream outputStream=新的BufferedOutputStream(
新文件outputstream(outputFile));
试一试{
}最后{
outputStream.flush();
outputStream.close();
inputStream.close();
}
}
私有void createDir(文件目录){
if(dir.exists()){
回来
}
如果(!dir.mkdirs()){
抛出新的运行时异常(“无法创建目录”+dir);
}
}
}
私有void递归删除(文件或目录){
if(fileOrDirectory.isDirectory())
对于(文件子项:fileOrDirectory.listFiles())
递归删除(子级);
fileOrDirectory.delete();
}
}使用以下类别:-
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();
}
}
}
公共类解压缩
{
私有上下文;
私人记录器;
公共解压缩(上下文)
{
this.mContext=上下文;
mLogger=Logger.getInstance(mContext);
}
/**
*在指定位置解压给定的zip文件。
*
*@param-zipFile
*
*传递zip文件路径-将在文件目录中搜索文件。
*传递zip文件名(不带扩展名)-将在应用程序中搜索
*原始文件夹。
*
*@param解压
*
*将解压缩“zipFile”的传递路径位置。
*/
@抑制警告(“资源”)
public void解压(字符串zipFile、字符串解压)
{
尝试
{
System.out.println(“>>>开始解压缩…”+Calendar.getInstance().getTime());
InputStream fin=null;
尝试
{
fin=新文件输入流(zipFile);
}
捕获(例外e)
{
int resId=mContext.getApplicationContext().getResources().getIdentifier(zipFile,“raw”,mContext.getApplicationContext().getPackageName());
fin=mContext.getResources().openRawResource(resId);
}
ZipInputStream zin=新的ZipInputStream(fin);
ZipEntry ze=null;
整数计数;
while((ze=zin.getnextery())!=null)
{
v(“解压缩”、“解压缩”+ze.getName());
if(ze.isDirectory())
{
目录检查器(ze.getName(),unZipLocation);
}
其他的
{
FileOutputStream fout=新的FileOutputStream(unZipLocation+ze.getName());
字节数据[]=新字节[128*1024];
而((计数=zin.read(数据))!=-1)
{
四次写入(数据,0,计数);
}
数据=空;