Commit cb2dcbd1 authored by fallenstardust's avatar fallenstardust

upzip

parent 4ca7bb81
...@@ -30,6 +30,7 @@ import cn.garymb.ygomobile.utils.DownloadUtil; ...@@ -30,6 +30,7 @@ import cn.garymb.ygomobile.utils.DownloadUtil;
import cn.garymb.ygomobile.utils.FileUtils; import cn.garymb.ygomobile.utils.FileUtils;
import cn.garymb.ygomobile.utils.UnzipUtils; import cn.garymb.ygomobile.utils.UnzipUtils;
import cn.garymb.ygomobile.utils.YGOUtil; import cn.garymb.ygomobile.utils.YGOUtil;
import ocgcore.DataManager;
import ocgcore.data.Card; import ocgcore.data.Card;
public class WebActivity extends BaseActivity { public class WebActivity extends BaseActivity {
...@@ -59,13 +60,16 @@ public class WebActivity extends BaseActivity { ...@@ -59,13 +60,16 @@ public class WebActivity extends BaseActivity {
case ZIP_READY: case ZIP_READY:
btn_download.setText(R.string.title_use_ex); btn_download.setText(R.string.title_use_ex);
break; break;
case ZIP_UPDATE_PATH_PROGRESS:
btn_download.setText(msg.obj.toString());
break;
case ZIP_UNZIP_OK: case ZIP_UNZIP_OK:
Intent startSetting = new Intent(getContext(), SettingsActivity.class); if (!AppsSettings.get().isReadExpansions()) {
startActivity(startSetting); Intent startSetting = new Intent(getContext(), SettingsActivity.class);
Toast.makeText(getContext(), R.string.ypk_go_setting, Toast.LENGTH_LONG).show(); startActivity(startSetting);
Toast.makeText(getContext(), R.string.ypk_go_setting, Toast.LENGTH_LONG).show();
} else {
DataManager.get().load(true);
Toast.makeText(getContext(), R.string.ypk_installed, Toast.LENGTH_LONG).show();
}
btn_download.setVisibility(View.GONE);
break; break;
case ZIP_UNZIP_EXCEPTION: case ZIP_UNZIP_EXCEPTION:
Toast.makeText(getContext(), getString(R.string.install_failed_bcos) + msg.obj, Toast.LENGTH_SHORT).show(); Toast.makeText(getContext(), getString(R.string.install_failed_bcos) + msg.obj, Toast.LENGTH_SHORT).show();
...@@ -209,9 +213,11 @@ public class WebActivity extends BaseActivity { ...@@ -209,9 +213,11 @@ public class WebActivity extends BaseActivity {
Message message = new Message(); Message message = new Message();
message.what = ZIP_READY; message.what = ZIP_READY;
try { try {
UnzipUtils.unZipFolder(file.toString(), AppsSettings.get().getResourcePath()); UnzipUtils.upZipFile(file, AppsSettings.get().getResourcePath());
} catch (Exception e) { } catch (Exception e) {
message.what = ZIP_UNZIP_EXCEPTION; message.what = ZIP_UNZIP_EXCEPTION;
} finally {
message.what = ZIP_UNZIP_OK;
} }
handler.sendMessage(message); handler.sendMessage(message);
} }
......
package cn.garymb.ygomobile.utils; package cn.garymb.ygomobile.utils;
import android.content.Context;
import com.file.zip.ZipEntry;
import com.file.zip.ZipFile;
import com.file.zip.ZipOutputStream;
import com.yuyh.library.imgsel.utils.LogUtils;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.Enumeration;
import java.util.zip.ZipInputStream; import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import cn.garymb.ygomobile.lite.R;
public class UnzipUtils { public class UnzipUtils {
public UnzipUtils(){ private static final int BUFFER_SIZE = 1024 * 1024;//1M Byte
}
/**
* DeCompress the ZIP to the path
* @param zipFileString name of ZIP
* @param outPathString path to be unZIP
* @throws Exception
*/
public static void unZipFolder(String zipFileString, String outPathString) throws Exception {
ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString));
ZipEntry zipEntry;
String szName = "";
while ((zipEntry = (ZipEntry) inZip.getNextEntry()) != null) {
szName = zipEntry.getName();
if (zipEntry.isDirectory()) {
// get the folder name of the widget
szName = szName.substring(0, szName.length() - 1);
File folder = new File(outPathString + File.separator + szName);
folder.mkdirs();
} else {
File file = new File(outPathString + File.separator + szName);
file.createNewFile();
// get the output stream of the file
FileOutputStream out = new FileOutputStream(file);
int len;
byte[] buffer = new byte[1024];
// read (len) bytes into buffer
while ((len = inZip.read(buffer)) != -1) {
// write (len) byte from buffer at the position 0
out.write(buffer, 0, len);
out.flush();
}
out.close();
}
}
LogUtils.e(R.string.done);
inZip.close();
}
/**
* Compress file and folder
* @param srcFileString file or folder to be Compress
* @param zipFileString the path name of result ZIP
* @throws Exception
*/
public static void ZipFolder(String srcFileString, String zipFileString)throws Exception {
//create ZIP
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString));
//create the file
File file = new File(srcFileString);
//compress
ZipFiles(file.getParent()+File.separator, file.getName(), outZip);
//finish and close
outZip.finish();
outZip.close();
}
/** /**
* compress files * 解压缩一个文件
* @param folderString *
* @param fileString * @param zipFile 压缩文件
* @param zipOutputSteam * @param folderPath 解压缩的目标目录
* @throws Exception * @return
* @throws IOException 当解压缩过程出错时抛出
*/ */
private static void ZipFiles(String folderString, String fileString, ZipOutputStream zipOutputSteam)throws Exception{ public static ArrayList<File> upZipFile(File zipFile, String folderPath) throws IOException {
if(zipOutputSteam == null) ArrayList<File> fileList = new ArrayList<File>();
return; File desDir = new File(folderPath);
File file = new File(folderString+fileString); if (!desDir.exists()) {
if (file.isFile()) { desDir.mkdirs();
ZipEntry zipEntry = new ZipEntry(fileString);
FileInputStream inputStream = new FileInputStream(file);
zipOutputSteam.putNextEntry(zipEntry);
int len;
byte[] buffer = new byte[4096];
while((len=inputStream.read(buffer)) != -1)
{
zipOutputSteam.write(buffer, 0, len);
}
zipOutputSteam.closeEntry();
} }
else { ZipFile zf = new ZipFile(zipFile);
//folder for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements(); ) {
String fileList[] = file.list(); ZipEntry entry = (ZipEntry) entries.nextElement();
//no child file and compress if (entry.isDirectory()) {
if (fileList.length <= 0) { continue;
ZipEntry zipEntry = new ZipEntry(fileString+File.separator); }
zipOutputSteam.putNextEntry(zipEntry); InputStream is = zf.getInputStream(entry);
zipOutputSteam.closeEntry(); String str = folderPath + File.separator + entry.getName();
str = new String(str.getBytes("8859_1"), "UTF-8");
File desFile = new File(str);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
desFile.createNewFile();
} }
//child files and recursion OutputStream os = new FileOutputStream(desFile);
for (int i = 0; i < fileList.length; i++) { byte buffer[] = new byte[BUFFER_SIZE];
ZipFiles(folderString, fileString+java.io.File.separator+fileList[i], zipOutputSteam); int length;
}//end of for while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
}
os.flush();
os.close();
is.close();
fileList.add(desFile);
} }
return fileList;
} }
/** /**
* return the InputStream of file in the ZIP * 解压文件名包含传入文字的文件
* @param zipFileString name of ZIP *
* @param fileString name of file in the ZIP * @param zipFile 压缩文件
* @return InputStream * @param folderPath 目标文件夹
* @throws Exception * @param nameContains 传入的文件匹配名
*/
public static InputStream UnZip(String zipFileString, String fileString)throws Exception {
ZipFile zipFile = new ZipFile(zipFileString);
ZipEntry zipEntry = zipFile.getEntry(fileString);
return zipFile.getInputStream(zipEntry);
}
/**
* return files list(file and folder) in the ZIP
* @param zipFileString ZIP name
* @param bContainFolder contain folder or not
* @param bContainFile contain file or not
* @return * @return
* @throws Exception * @throws IOException 当解压缩过程出错时抛出
*/ */
public static List<File> GetFileList(String zipFileString, boolean bContainFolder, boolean bContainFile)throws Exception { public static ArrayList<File> upZipSelectFile(File zipFile, String folderPath, String nameContains) throws IOException {
List<File> fileList = new ArrayList<File>(); ArrayList<File> fileList = new ArrayList<File>();
ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFileString)); File desDir = new File(folderPath);
ZipEntry zipEntry; if (!desDir.exists()) {
String szName = ""; desDir.mkdirs();
while ((zipEntry = (ZipEntry) inZip.getNextEntry()) != null) { }
szName = zipEntry.getName(); ZipFile zf = new ZipFile(zipFile);
if (zipEntry.isDirectory()) { for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements(); ) {
// get the folder name of the widget ZipEntry entry = (ZipEntry) entries.nextElement();
szName = szName.substring(0, szName.length() - 1); if (entry.isDirectory()) {
File folder = new File(szName); continue;
if (bContainFolder) { }
fileList.add(folder); if (entry.getName().contains(nameContains)) {
InputStream is = zf.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
str = new String(str.getBytes("8859_1"), "UTF-8");
File desFile = new File(str);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
desFile.createNewFile();
} }
OutputStream os = new FileOutputStream(desFile);
} else { byte buffer[] = new byte[BUFFER_SIZE];
File file = new File(szName); int length;
if (bContainFile) { while ((length = is.read(buffer)) > 0) {
fileList.add(file); os.write(buffer, 0, length);
} }
is.close();
os.flush();
os.close();
fileList.add(desFile);
} }
} }
inZip.close();
return fileList; return fileList;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment