Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;
import java.io.DataOutputStream;

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;

import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

public class Main {
    public static void UpdateAnimationFromPath(String sFromPath) {
        String sTmpZipFile = "/data/local/tmp/z4res.zip";
        String sOrgZipFile = "/system/framework/framework-res.apk";
        try {
            RunRootCmd("busybox cp " + sOrgZipFile + " " + sTmpZipFile);
            File fNewZipFile = UpdateZipFromPath(sTmpZipFile, sFromPath);
            RunSystemCmd("busybox cp " + fNewZipFile.getPath() + " " + sOrgZipFile + "; busybox rm " + sTmpZipFile);
            fNewZipFile.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void RunRootCmd(String cmd) {
        try {
            //
            // run our command using 'su' to gain root
            //
            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream outputStream = new DataOutputStream(process.getOutputStream());

            outputStream.writeBytes(cmd + "\n");
            outputStream.flush();

            outputStream.writeBytes("exit\n");
            outputStream.flush();
            process.waitFor();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static File UpdateZipFromPath(String sZipFile, String sPath) throws Exception {
        File tmpFile = File.createTempFile("z4zip-tmp-", ".zip");
        tmpFile.deleteOnExit();
        ZipFile inZip = new ZipFile(sZipFile);
        ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(tmpFile.getPath()));

        Enumeration<? extends ZipEntry> entries = inZip.entries();
        while (entries.hasMoreElements()) {
            ZipEntry e = entries.nextElement();
            outZip.putNextEntry(e);
            if (!e.isDirectory()) {
                File f = new File(sPath + "/" + e.getName());
                if (f.exists()) {
                    copy(new FileInputStream(f.getPath()), outZip);
                } else {
                    copy(inZip.getInputStream(e), outZip);
                }
            }
            outZip.closeEntry();
        }
        inZip.close();
        outZip.close();
        return tmpFile;
    }

    public static void RunSystemCmd(String cmd) {
        String command = new String(
                "busybox mount -o remount,rw,llw,check=no /system || busybox mount -o remount,rw /system; " + cmd
                        + ";" + "busybox mount -o remount,ro,llw,check=no /system");
        RunRootCmd(command);
    }

    public static void copy(InputStream input, OutputStream output) throws IOException {
        // 32kb buffer
        final byte[] BUFFER = new byte[32 * 1024];
        int bytesRead;
        while ((bytesRead = input.read(BUFFER)) != -1) {
            output.write(BUFFER, 0, bytesRead);
        }
    }
}