에이치의 모바일 앱 개발
DBunzip version 1.0.1 본문
/**
* Created by lsh on 2016-06-29.
* version 1.0.1
*/
public class DBunzip extends AsyncTask<Void, Void, Boolean> {
private String zipFile; //저장된 zip 파일 위치
private String localFolderPath; //압출을 풀 위치
private Context context;
private int version;
public DBunzip(Context context, int version, String zipFile, String localFolderPath) {
this.context = context;
this.version = version;
this.zipFile = zipFile;
this.localFolderPath = localFolderPath;
}
//변수 location에 저장된 directory의 폴더를 만듭니다.
private void _dirChecker(String dir) {
File f = new File(localFolderPath + dir);
if (!f.isDirectory()) {
f.mkdirs();
}
}
@Override
protected Boolean doInBackground(Void... params) {
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()); //압축이 풀리면서 logcat으로 zip 안에 있던 파일
if (ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
FileOutputStream fout = new FileOutputStream(localFolderPath + "/" + ze.getName());
BufferedInputStream in = new BufferedInputStream(zin); //이렇게 지정하지 않고 unzip을
BufferedOutputStream out = new BufferedOutputStream(fout);// 수행하면 속도가 매우 느려
byte bytes[] = new byte[1024]; // 집니다.
int n;
while ((n = in.read(bytes)) != -1) {
fout.write(bytes, 0, n);
}
zin.closeEntry();
fout.close();
rename(ze.getName());
}
}
zin.close();
return true;
} catch (Exception e) {
Log.e("Decompress", "unzip", e);
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
if (result) {
Log.e("unzip", "success");
IntroActivity.AppStart(context);
DBVersionWrite(version);
} else {
Log.e("unzip", "fail");
IntroActivity.AppFinish(context);
}
}
private void rename(String befoName) {
File befoFileName = new File(localFolderPath + "/" + befoName);
File changeFileName = new File(localFolderPath + "/" + "aftername.sqlite");
befoFileName.getParentFile().exists();
befoFileName.exists();
befoFileName.renameTo(changeFileName);
}
private void getfilesname() {
File[] listFiles = (new File(localFolderPath).listFiles());
for (File file : listFiles)
Log.e("filename", localFolderPath + "/" + file.getName());
}
public void DBVersionWrite(int version) {
SharedPreferences sharedPreferences;
SharedPreferences.Editor sharedPreferencesEditor;
sharedPreferences = context.getSharedPreferences("appleface", Context.MODE_PRIVATE);
sharedPreferencesEditor = sharedPreferences.edit();
sharedPreferencesEditor.putInt("appleDB", version);
sharedPreferencesEditor.commit();
}
}
'Android > Android 개발 소스' 카테고리의 다른 글
uncaughtexception version 1.0.2 (0) | 2017.12.23 |
---|---|
File & I/O (0) | 2017.12.23 |
DBFileDownload version 1.0.1 (0) | 2017.12.23 |
SQLiteOpenHelper 1.0.1 (0) | 2017.12.23 |
ripple (0) | 2017.12.23 |