package org.tizen.installmanager.lib.win;
import java.io.File;
-import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
+import java.util.zip.ZipException;
+import java.util.zip.ZipFile;
import org.tizen.installmanager.core.IMFatalException;
-import org.tizen.installmanager.lib.ErrorController;
+import org.tizen.installmanager.lib.ErrorController.ErrorCode;
import org.tizen.installmanager.lib.IIMProgressMonitor;
import org.tizen.installmanager.lib.Log;
import org.tizen.installmanager.lib.SDKPackageFormat;
-import org.tizen.installmanager.lib.ErrorController.ErrorCode;
-import org.tizen.installmanager.util.PathUtil;
/**
* Manages to unpack package of tizen sdk for windows.
* @author Shihyun Kim <shihyun.kim@samsung.com>
+ * @author Moonkyu Kang <moonkyu82.kang@samsung.com>
*
*/
public class WindowsSDKPackageFormat extends SDKPackageFormat{
return ERROR;
}
Log.log(fileFullPath + " exists.");
-
- if (targetDir == null || !targetDir.isAbsolute()) {
- return ERROR;
- }
-
- Log.log(targetDir + " is not null and absolute.");
-
- if (!targetDir.exists()) {
- Log.log(targetDir + " does not exist. InstallManager will make target directory.");
- if (targetDir.mkdirs()) {
- Log.log("Success to create directory ==> " + targetDir);
- } else {
- Log.err("Fail to create directory ==> " + targetDir);
- }
- }
-
- long ret = unZipPackage(fileFullPath, targetDir, monitor);
-
- closeFileOutput();
- return ret;
+ return unZipPackage(fileFullPath, targetDir, monitor);
}
-
- private long unZipPackage(File fileFullPath, File targetDir, IIMProgressMonitor monitor) {
- ZipInputStream zipInStream = null;
- FileOutputStream zipOutput = null;
- ZipEntry zipEntry = null;
-
- try {
- zipInStream = new ZipInputStream(new FileInputStream(fileFullPath));
- zipEntry = zipInStream.getNextEntry();
-
- long totalUnZipSize = 0;
- while (zipEntry != null) {
- if (monitor != null) {
- String fileName = PathUtil.getFileName(zipEntry.getName());
- monitor.workedSubTitle(fileName);
- }
-
- String targetPath = targetDir.getAbsolutePath() + File.separator + zipEntry.getName();
-
- // make directory
- File targetFile = new File(targetPath);
- if (targetFile.exists() && targetFile.isDirectory()) {
- writeFileList(zipEntry.getName());
- zipEntry = zipInStream.getNextEntry();
- continue;
- }
-
- File parentFile = targetFile.getParentFile();
- if (!parentFile.exists()) {
- if (parentFile.mkdirs()) {
- Log.log("Success to create parent directory ==> " + parentFile);
- } else {
- Log.err("Fail to create parent directory ==> " + parentFile);
- }
- }
-
- if (targetPath.endsWith("/")) {// zipEntry.isDirectory
- File target = new File(targetPath);
- if (target.mkdir()) {
- Log.log("Success to create target directory ==> " + target);
- } else {
- Log.err("Fail to create target directory ==> " + target);
- }
- zipInStream.closeEntry();
- writeFileList(zipEntry.getName());
- zipEntry = zipInStream.getNextEntry();
- continue;
- }
-
- zipOutput = new FileOutputStream(targetPath);
-
- byte[] zipBuf = new byte[BUF_SIZE];
- int readByte = 0;
-
- while ((readByte = zipInStream.read(zipBuf)) >= 0) {
- zipOutput.write(zipBuf, 0, readByte);
- totalUnZipSize += readByte;
- }
-
- writeFileList(zipEntry.getName());
- if (zipOutput != null) {
- zipOutput.close();
- }
-
- if (zipEntry != null) {
- zipInStream.closeEntry();
- }
- zipEntry = zipInStream.getNextEntry();
- }
-
- return totalUnZipSize;
- } catch (IOException e) {
- Log.ExceptionLog(e);
- if (zipEntry != null) {
- Log.err("Fail to unpack " + zipEntry.getName());
- }
-
- return ERROR;
- } catch (Throwable e) {
- Log.ExceptionLog(e);
- return ERROR;
- } finally {
- try {
- if (zipOutput != null) {
- zipOutput.flush();
- zipOutput.close();
- }
- } catch (IOException e) {
- if (zipEntry != null) {
- Log.err("Fail to close output stream : "
- + zipEntry.getName());
- } else {
- Log.ExceptionLog(e);
- }
- }
-
- try {
- if (zipInStream != null) {
- zipInStream.closeEntry();
- }
- } catch (IOException e) {
- if (zipEntry != null) {
- Log.err("Fail to close output stream : "
- + zipEntry.getName());
- } else {
- Log.ExceptionLog(e);
- }
- }
-
- if (zipInStream != null) {
- try {
- zipInStream.close();
- } catch (IOException e) {
- Log.ExceptionLog(e);
- }
- }
-
- try {
- if (mFileOutput != null) {
- mFileOutput.close();
- }
- } catch (IOException e) {
- if (zipEntry != null) {
- Log.err("Fail to close output stream : "
- + zipEntry.getName());
- } else {
- Log.ExceptionLog(e);
- }
- }
+ private long unZipPackage(File fileFullPath, File targetDir, IIMProgressMonitor monitor) {
+
+ ZipFile zipFile = null;
+ FileOutputStream zipOutStream = null;
+ InputStream zis = null;
+ long totalUnZipSize = 0;
+ List<String> installedFileList = new ArrayList<String>();
+
+ try {
+ zipFile = new ZipFile(fileFullPath);
+ @SuppressWarnings("unchecked")
+ Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zipFile
+ .entries();
+ while (entries.hasMoreElements()) {
+ ZipEntry zipEntry = entries.nextElement();
+
+ if (zipEntry.isDirectory()) {
+ installedFileList.add(zipEntry.getName());
+ continue;
+ } else {
+ // Unzip files in "data" directory to target directory
+ String targetPathWithOutTempDir = targetDir.getParent();
+ String targetPath = null;
+ if (zipEntry.getName().startsWith("data/")) {
+ String fileName = zipEntry.getName().substring(5);
+ targetPath = targetPathWithOutTempDir + File.separator
+ + fileName;
+ } else {
+ targetPath = targetDir.getAbsolutePath()
+ + File.separator + zipEntry.getName();
+ }
+
+ // Prepare file for write
+ File targetFile = new File(targetPath);
+
+ // create parent directory
+ File parentFile = targetFile.getParentFile();
+ if (!parentFile.exists()) {
+ if (parentFile.mkdirs()) {
+ Log.log("Success to create parent directory ==> "
+ + parentFile);
+ } else {
+ Log.err("Fail to create parent directory ==> "
+ + parentFile);
+ }
+ }
+
+ // Write zip file to target File
+
+ zipOutStream = new FileOutputStream(targetPath);
+ zis = zipFile.getInputStream(zipEntry);
+ byte[] zipBuf = new byte[BUF_SIZE];
+ int readByte = 0;
+
+ while ((readByte = zis.read(zipBuf)) >= 0) {
+ zipOutStream.write(zipBuf, 0, readByte);
+ totalUnZipSize += readByte;
+ }
+
+ installedFileList.add(zipEntry.getName());
+
+ if (zipOutStream != null) {
+ zipOutStream.close();
+ }
+
+ if (zis != null) {
+ zis.close();
+ }
+ }
+ }
+ writeFileList(installedFileList);
+ } catch (ZipException e1) {
+ Log.ExceptionLog(e1);
+ } catch (IOException e1) {
+ Log.ExceptionLog(e1);
+ }finally{
+ try {
+ if(zis != null && zis.available() > 0){
+ try {
+ zis.close();
+ } catch (IOException e) {
+ Log.ExceptionLog(e);
+ }
+ }
+ } catch (IOException e) {
+ Log.ExceptionLog(e);
+ }
+
+ if(zipFile != null){
+ try {
+ zipFile.close();
+ } catch (IOException e) {
+ Log.ExceptionLog(e);
+ }
+ }
+ if(zipOutStream != null){
+ try {
+ zipOutStream.flush();
+ zipOutStream.close();
+ } catch (IOException e) {
+ Log.ExceptionLog(e);
+ }
+ }
+ closeFileOutput();
+ }
+ closeFileOutput();
+ return totalUnZipSize;
+ }
- closeFileOutput();
- }
- }
- private void writeFileList(String filePath) {
- if (filePath == null || filePath.equals("")) {
- return;
- }
-
- filePath = removeDataDirectory(filePath);
- if (filePath == null || filePath.equals("")) {
- return;
- }
-
- if (mFileOutput == null) {
- return;
- }
-
- try {
- mFileOutput.write((filePath + "\n").getBytes());
- } catch (IOException e) {
- Log.ExceptionLog(e);
- Log.err("Fail to unpack " + filePath);
- throw new IMFatalException(ErrorCode.CANNOT_UNPACK_PACKAGE);
- }
- }
+ private void writeFileList(List<String> installedFileList) {
+
+ for (String filePath : installedFileList) {
+ if (filePath == null || filePath.equals("")) {
+ continue;
+ }
+
+ filePath = removeDataDirectory(filePath);
+ if (filePath == null || filePath.equals("")) {
+ continue;
+ }
+
+ if (mFileOutput == null) {
+ continue;
+ }
+
+ try {
+ mFileOutput.write((filePath + "\n").getBytes());
+ } catch (IOException e) {
+ Log.ExceptionLog(e);
+ Log.err("Fail to unpack " + filePath);
+ throw new IMFatalException(ErrorCode.CANNOT_UNPACK_PACKAGE);
+ }
+ }
+ }
+
private String removeDataDirectory(String filePath) {
if (filePath == null || filePath.equals("")) {