로컬 zip 압축을 풀고 데이터 읽기 업데이트 UI
우선 목록 파일에 권한을 추가하려면 다음과 같이 하십시오.
그런 다음 zip 도구 클래스를 생성합니다.
public class ZipFloderUtil {
/**
* . zipFile folderPath .
*
* @throws Exception
*/
public static void upZipFile(String zipFile, String folderPath)
throws ZipException, IOException {
ZipFile zfile = new ZipFile(zipFile);
Enumeration zList = zfile.entries();
ZipEntry ze = null;
byte[] buf = new byte[1024];
while (zList.hasMoreElements()) {
ze = (ZipEntry) zList.nextElement();
if (ze.isDirectory()) {
Log.d("upZipFile", "ze.getName() = " + ze.getName());
String dirstr = folderPath + ze.getName();
// dirstr.trim();
dirstr = new String(dirstr.getBytes("8859_1"), "GB2312");
Log.d("upZipFile", "str = " + dirstr);
File f = new File(dirstr);
f.mkdir();
continue;
}
Log.d("upZipFile", "ze.getName() = " + ze.getName());
OutputStream os = new BufferedOutputStream(new FileOutputStream(
getRealFileName(folderPath, ze.getName())));
InputStream is = new BufferedInputStream(zfile.getInputStream(ze));
int readLen = 0;
while ((readLen = is.read(buf, 0, 1024)) != -1) {
os.write(buf, 0, readLen);
}
is.close();
os.close();
}
zfile.close();
Log.d("upZipFile", "finishssssssssssssssssssss");
}
/**
* , .
*
* @param baseDir
*
* @param absFileName
* , ZipEntry name
* @return java.io.File
*/
public static File getRealFileName(String baseDir, String absFileName) {
String[] dirs = absFileName.split("/");
File ret = new File(baseDir);
String substr = null;
if (dirs.length > 1) {
for (int i = 0; i < dirs.length - 1; i++) {
substr = dirs[i];
try {
// substr.trim();
substr = new String(substr.getBytes("8859_1"), "GB2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ret = new File(ret, substr);
}
Log.d("upZipFile", "1ret = " + ret);
if (!ret.exists())
ret.mkdirs();
substr = dirs[dirs.length - 1];
try {
// substr.trim();
substr = new String(substr.getBytes("8859_1"), "GB2312");
Log.d("upZipFile", "substr = " + substr);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ret = new File(ret, substr);
Log.d("upZipFile", "2ret = " + ret);
return ret;
}
return ret;
}
}
자바에서 통합된 zip 도구를 사용하여 압축을 풀 수 있습니다.첫 번째 방법은 압축을 풀고 하나의 경로에 저장하는 것이고, 두 번째 방법은 압축을 풀고 저장한 데이터를 읽는 것이다.여기 있는 데모는 사진이기 때문에 BitmapFactory를 사용합니다
String zipPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "myfile.zip";
String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "file";
imageView = (ImageView) findViewById(R.id.imageView);
try {
ZipFloderUtil.upZipFile(zipPath,filePath);
Bitmap bitmap = BitmapFactory.decodeFile(filePath+File.separator+"myskin/main.jpg");
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
중요한 부분은 이미 모두 붙여졌다.
git :https://github.com/SingleShu/UnZip
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.