您的位置:首页 > 运维架构 > Linux

代码中执行命令处理(面向Linux)

2008-09-17 10:38 393 查看
删除文件:

                String filePath = "";

                String cmd = "rm -rf " + filePath;
                Process process = Runtime.getRuntime().exec(cmd);

                int cmdvalue = process.waitFor();//等子进程执行完即命令执行完毕返回才继续执行
                if (cmdvalue == 0)
                {
                   //处理
                }

 

文件解压缩:

public static void unzipFile(String dir, String fileurl) throws Exception
    {
        int buffer = 1024;

        File rootdir = new File(dir);
        if (!rootdir.exists())
        {
            track.info(Helper.class, "unzipFile() create dir :" + rootdir,
                       log, Logger.DEBUG);

            rootdir.mkdirs();
        }
        try
        {
            BufferedOutputStream dest = null;

            FileInputStream fis = new FileInputStream(fileurl);
            ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
            ZipEntry entry;
            while ((entry = zis.getNextEntry()) != null)
            {
                track.info(Helper.class, "unzipFile() extracting: " + entry,
                           log, Logger.DEBUG);
                int count;
                byte data[] = new byte[buffer];

                if (entry.isDirectory() || entry.getName().endsWith("//"))
                {
                    // write the files to the disk
                    //是目录!
                    File subdir = null;
                    if (entry.getName().endsWith("//"))
                    {
                        track.info(Helper.class, "isdirectory! and endswith //!!!");
                        String entrynametemp = entry.getName().replaceAll("//",
                            "/");
                        subdir = new File(dir + entrynametemp);
                    } else
                    {
                        subdir = new File(dir + entry.getName());
                    }
                    if (!subdir.exists())
                    {
                        subdir.mkdirs();

                        track.info(Helper.class,
                                   "unzipFile() create sub_dir:"
                                   + dir + entry.getName());
                    }
                } else
                {
                    //是文件!
                    FileOutputStream fos = new FileOutputStream(
                        dir + entry.getName());

                    track.info(Helper.class, "unzipFile() creating files : "
                               + dir + entry.getName(), log, Logger.DEBUG);

                    dest = new BufferedOutputStream(fos, buffer);
                    while ((count = zis.read(data, 0, buffer)) != -1)
                    {
                        dest.write(data, 0, count);
                    }
                    dest.flush();
                    dest.close();
                }
            }
            zis.close();
        } catch (Exception ex)
        {
            //异常处理。
        }
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息