Util--UnzipUtil

开发者博客:[url]http://www.developsearch.com[/url]

/**
* 解压缩帮助类
*
* @author chenxin
* @version [版本号, 2012-5-21]
* @see [相关类/方法]
* @since [产品/模块版本]
*/
public class UnzipUtil {

/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(UnzipContentHelper.class);

/**
*
* 解开压缩
*
* @param contentNo contentno
* @param sourcePath 原epub书籍路劲
* @param targetPath 目标epub书籍路劲 只到保存跟路径
* @see [类、类#方法、类#成员]
*/
public void unZip(String contentNo, String sourcePath, String targetPath)
throws Exception
{

// 查看资源是否存在
File resourceFile = new File(sourcePath);
// 如果资源不存在 就抛出异常
if (!resourceFile.exists())
{

throw new Exception("资源 " + resourceFile.getPath() + " 不存在");

}

File targetFileDir = new File(targetPath);
// 保存目录是否存在
if (!targetFileDir.exists())
{
if (logger.isDebugEnabled())
logger.debug("!targetFileDir.exists()=" + targetFileDir.getPath());

targetFileDir.mkdir();
}

// 创建临时目录
File targetFileDirEpub = new File(targetPath + "/" + contentNo + "_temp");
if (!targetFileDirEpub.exists())
{
if (logger.isDebugEnabled())
logger.debug(targetFileDirEpub.getPath());
targetFileDirEpub.mkdir();
}

// 组装shell脚本
File file = File.createTempFile("unZip", ".sh");
write(file, contentNo, sourcePath, targetPath);

// 复制文件
int retVal = shell("sh " + file.getPath());

// 如果不等于0 表示执行失败
if (logger.isDebugEnabled())
logger.debug("retVal=" + retVal);
if (retVal != 0)
{
throw new Exception("脚本执行出错");
}

}

private int shell(String cmd)
{
int retVal = 1;
if (logger.isInfoEnabled())
logger.info(cmd);
try
{
Process process = java.lang.Runtime.getRuntime().exec(cmd);
retVal = process.waitFor();
}
catch (Exception e)
{

e.printStackTrace();
}

return retVal;
}

private void write(File file, String contentNo, String sourcePath, String targetPath)
{
FileWriter fw = null;
try
{
fw = new FileWriter(file);
fw.write("cp " + sourcePath + " " + targetPath + "/" + contentNo + "_temp/");
fw.write("\n");

fw.write("cd " + targetPath + "/" + contentNo + "_temp");
fw.write("\n");

fw.write("unzip -o -q *.epub");
fw.write("\n");

fw.write("rm *.epub");
fw.write("\n");

fw.write("cd ..");
fw.write("\n");

fw.write("rm -fR " + contentNo);
fw.write("\n");

fw.write("mv " + contentNo + "_temp " + contentNo);
fw.write("\n");
}
catch (IOException e)
{

e.printStackTrace();
}
finally
{

try
{
if (fw != null)
fw.close();
}
catch (IOException e)
{

e.printStackTrace();
}
}

}

public static void main(String[] args)
{
// /data/lgftp/resource/2011/2/22/7a8a51bb-aaad-4f12-8bb1-3a18140fe9a1.epub
UnzipContentHelper unzip = new UnzipContentHelper();
try
{
unzip.unZip("123124",
"/data/lgftp/resource/2011/2/22/7a8a51bb-aaad-4f12-8bb1-3a18140fe9a1.epub",
"/data/lgftp/ebook");
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值