Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to achieve 7z format compression?
#2
Use 7z to achieve file compression and decompression of the code Idea

/**
* <p><b>Copyright: All Rights Reserved © 2015-2016</b></p>
* <p><b>Company:Travelsky</b></p>
* @ClassName: Zip7ZUtil
* @date 2015-5-14 2:06:25
*/
public class Zip7zUtil {
private static String exePath=SysPropUtil.getValue("7z.path");
private static Logger logger = LogManager.getLogger(Zip7zUtil.class);
public static void zip(String srcRootPath,String zipFile) {
String cmd = "7z a -tzip " + zipFile + " " + srcRootPath;
try {
Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* @param zipFilePath
* @param unZipPath
*/
/*public static boolean syncUnzip(String zipFilePath,String unZipPath) {
logger.info("unzip "+zipFilePath+" to "+unZipPath);
boolean result;
String cmd = exePath+"\\7z x -o" + unZipPath + " -y " + zipFilePath;
logger.info("do syncUnzip exec : "+cmd);
try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while (true) {
try {
line = br.readLine();
if (line == null) {
result = true;
break;
}else{
System.out.println(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(" syncUnzip exception : "+e);
result = false;
}

}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(" syncUnzip exception : "+e);
result = false;
}
return result;
}*/

public static boolean syncUnzip(String zipFilePath,String unZipPath) {
logger.info("unzip "+zipFilePath+" to "+unZipPath);
boolean result;
String cmd = exePath+"\\7z x -o" + unZipPath + " -y " + zipFilePath;
logger.info("do syncUnzip exec : "+cmd);
try {
File zipFile=new File(zipFilePath);
long zipSize=zipFile.length();
long unzipSize=0;
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while (true) {
try {
line = br.readLine();
logger.info("7z info : " + line);
if (line != null) {

String[] arr=line.split("=");
if (arr.length==2) {
if (arr[0].trim().equals("Physical Size")) {
zipSize=Long.parseLong(arr[1].trim());
}
}
String[] arr2=line.split(":");
if (arr2[0].trim().equals("Compressed")) {
unzipSize=Long.parseLong(arr2[1].trim());
}
}else{
logger.info("unzipSize = "+unzipSize+" , zipSize = "+zipSize);
if (unzipSize==zipSize) {
result = true;
}else{
result = false;
logger.error(" syncUnzip "+zipFilePath+" to "+unZipPath+" failed ! ");
}
break;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(" syncUnzip exception : "+e);
result = false;
}

}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error(" syncUnzip exception : "+e);
result = false;
}
return result;
}
/*
* @param zipFilePath
* @param unZipPath
* @param listener
*/
public static void asyncUnzip(String zipFilePath,String unZipPath,final ICallbackListener listener) {
if (zipFilePath == null || zipFilePath.equals("") || unZipPath == null || unZipPath.equals("")) {
listener.callback("fail");
return;
}
String cmd = exePath+"\\7z x -o" + unZipPath + " -y -tzip " + zipFilePath;
try {
final Process p = Runtime.getRuntime().exec(cmd);
if (listener != null) {
new Thread() {
public void run() {
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
while (true) {
try {
line = br.readLine().trim();
if (line == null) {
listener.callback("OK");
break;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
listener.callback("fail");
}

}
}
};
}


} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
listener.callback("fail");
}
}
}


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)