로컬 zip 압축을 풀고 데이터 읽기 업데이트 UI

4734 단어
오늘 백엔드에서 아이콘과 색깔의 zip을 주고 앞에서 사용하는 아이콘과 색깔 설정을 백엔드에서 읽어서 나중에 조정할 수 있도록 하는 요구를 만났습니다.아이콘이나 전체 색을 바꾸려면 백엔드에서 zip 버전을 직접 바꾸면 OS와 안드로이드가 모두 업데이트되고 앞에서 수정하지 않아도 된다. 이렇게 하면 일로 영일이라고 할 수 있다.많은 문제를 해결했다.그러면 이제 첫 번째 문제는 로컬 zip을 풀고 데이터를 얻는 것입니다.나는 여기에 데모를 하나 썼다.문서화:
우선 목록 파일에 권한을 추가하려면 다음과 같이 하십시오.
 
    
    

그런 다음 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

좋은 웹페이지 즐겨찾기