[INST] Change zip library for Windows 16/19816/2
authormoonkyu82.kang <moonkyu82.kang@samsung.com>
Mon, 21 Apr 2014 06:00:07 +0000 (15:00 +0900)
committermoonkyu82.kang <moonkyu82.kang@samsung.com>
Mon, 21 Apr 2014 06:00:07 +0000 (15:00 +0900)
Change-Id: I157a1910fd1c6765d981713f6a9b605141cdd21d
Signed-off-by: moonkyu82.kang <moonkyu82.kang@samsung.com>
InstallManager_java/src/org/tizen/installmanager/lib/win/WindowsSDKPackageFormat.java

index fd4025a..bf62407 100644 (file)
-/*\r
-*  InstallManager\r
-*\r
-* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.\r
-*\r
-* Contact: \r
-* Wooyoung Cho <wooyoung1.cho@samsung.com>\r
-* Shihyun Kim <shihyun.kim@samsung.com>\r
-* Taeyoung Son <taeyoung2.son@samsung.com>\r
-* Yongsung kim <yongsung1.kim@samsung.com>\r
-* \r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
-* you may not use this file except in compliance with the License.\r
-* You may obtain a copy of the License at\r
-*\r
-* http://www.apache.org/licenses/LICENSE-2.0\r
-*\r
-* Unless required by applicable law or agreed to in writing, software\r
-* distributed under the License is distributed on an "AS IS" BASIS,\r
-* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
-* See the License for the specific language governing permissions and\r
-* limitations under the License.\r
-*\r
-* Contributors:\r
-* - S-Core Co., Ltd\r
-*\r
-*/ \r
-\r
-package org.tizen.installmanager.lib.win;\r
-\r
-import java.io.File;\r
-import java.io.FileInputStream;\r
-import java.io.FileOutputStream;\r
-import java.io.IOException;\r
-import java.util.zip.ZipEntry;\r
-import java.util.zip.ZipInputStream;\r
-\r
-import org.tizen.installmanager.core.IMFatalException;\r
-import org.tizen.installmanager.lib.ErrorController;\r
-import org.tizen.installmanager.lib.IIMProgressMonitor;\r
-import org.tizen.installmanager.lib.Log;\r
-import org.tizen.installmanager.lib.SDKPackageFormat;\r
-import org.tizen.installmanager.lib.ErrorController.ErrorCode;\r
-\r
-\r
-/**\r
- * Manages to unpack package of tizen sdk for windows.\r
- * @author Shihyun Kim <shihyun.kim@samsung.com>\r
- *\r
- */\r
-public class WindowsSDKPackageFormat extends SDKPackageFormat{\r
-\r
-    final static int BUF_SIZE = 65536;\r
-    \r
-    private static final String DATA_DIRECTORY = "data/";\r
-    \r
-    @Override \r
-    public long unZip(File fileFullPath, File targetDir, IIMProgressMonitor monitor) {\r
-        Log.log("Unzip "+fileFullPath+" to "+targetDir);\r
-        \r
-        PackageType checkExtension = getPackageType(fileFullPath.getAbsolutePath());\r
-        \r
-        if (!fileFullPath.exists()) {\r
-               Log.err("Package file is not exsisting");\r
-               return ERROR;\r
-        } else if (checkExtension != PackageType.ZIP) {\r
-               Log.err("Package extension is not '.zip'");\r
-               return ERROR;\r
-        }\r
-        Log.log(fileFullPath + " exists.");\r
-               \r
-               if (targetDir == null || !targetDir.isAbsolute()) {\r
-                       return ERROR;\r
-               }\r
-               \r
-               Log.log(targetDir + " is not null and absolute.");\r
-               \r
-               if (!targetDir.exists()) {\r
-                       Log.log(targetDir + " does not exist. InstallManager will make target directory.");\r
-                       if (targetDir.mkdirs()) {\r
-                               Log.log("Success to create directory ==> " + targetDir);\r
-                       } else {\r
-                               Log.err("Fail to create directory ==> " + targetDir);\r
-                       }\r
-               }\r
-\r
-               long ret = unZipPackage(fileFullPath, targetDir, monitor);\r
-        \r
-        closeFileOutput(); \r
-        return ret;\r
-    }\r
-    \r
-       private long unZipPackage(File fileFullPath, File targetDir, IIMProgressMonitor monitor) {\r
-               ZipInputStream zipInStream = null;\r
-               FileOutputStream zipOutput = null;\r
-               ZipEntry zipEntry = null;\r
-\r
-               try {\r
-                       zipInStream = new ZipInputStream(new FileInputStream(fileFullPath));\r
-                       zipEntry = zipInStream.getNextEntry();\r
-\r
-                       long totalUnZipSize = 0;\r
-                       while (zipEntry != null) {\r
-                               String targetPath = targetDir.getAbsolutePath() + File.separator + zipEntry.getName();\r
-\r
-                               // make directory\r
-                               File targetFile = new File(targetPath);\r
-                               if (targetFile.exists() && targetFile.isDirectory()) {\r
-                                       writeFileList(zipEntry.getName());\r
-                                       zipEntry = zipInStream.getNextEntry();\r
-                                       continue;\r
-                               }\r
-\r
-                               File parentFile = targetFile.getParentFile();\r
-                               if (!parentFile.exists()) {\r
-                                       if (parentFile.mkdirs()) {\r
-                                               Log.log("Success to create parent directory ==> " + parentFile);\r
-                                       } else {\r
-                                               Log.err("Fail to create parent directory ==> " + parentFile);\r
-                                       }\r
-                               }\r
-\r
-                               if (targetPath.endsWith("/")) {// zipEntry.isDirectory\r
-                                       File target = new File(targetPath);\r
-                                       if (target.mkdir()) {\r
-                                               Log.log("Success to create target directory ==> " + target);\r
-                                       } else {\r
-                                               Log.err("Fail to create target directory ==> " + target);\r
-                                       }\r
-                                       zipInStream.closeEntry();\r
-                                       writeFileList(zipEntry.getName());\r
-                                       zipEntry = zipInStream.getNextEntry();\r
-                                       continue;\r
-                               }\r
-\r
-                               zipOutput = new FileOutputStream(targetPath);\r
-\r
-                               byte[] zipBuf = new byte[BUF_SIZE];\r
-                               int readByte = 0;\r
-\r
-                               while ((readByte = zipInStream.read(zipBuf)) >= 0) {\r
-                                       zipOutput.write(zipBuf, 0, readByte);\r
-                                       \r
-                                       if (monitor != null) {\r
-                                               monitor.workedInstallSize(readByte);\r
-                                       }\r
-                                       totalUnZipSize += readByte;\r
-                               }\r
-\r
-                               writeFileList(zipEntry.getName());\r
-\r
-                               if (zipOutput != null) {\r
-                                       zipOutput.close();\r
-                               }\r
-\r
-                               if (zipEntry != null) {\r
-                                       zipInStream.closeEntry();\r
-                               }\r
-                               zipEntry = zipInStream.getNextEntry();\r
-                       }\r
-\r
-                       return totalUnZipSize;\r
-               } catch (IOException e) {\r
-                       Log.ExceptionLog(e);\r
-                       if (zipEntry != null) {\r
-                               Log.err("Fail to unpack " + zipEntry.getName());\r
-                       }\r
-                       \r
-                       return ERROR;\r
-               } catch (Throwable e) {\r
-                       Log.ExceptionLog(e);\r
-                       return ERROR;\r
-               } finally {\r
-                       try {\r
-                               if (zipOutput != null) {\r
-                                       zipOutput.flush();\r
-                                       zipOutput.close();\r
-                               }\r
-                       } catch (IOException e) {\r
-                               if (zipEntry != null) {\r
-                                       Log.err("Fail to close output stream : "\r
-                                               + zipEntry.getName());\r
-                               } else {\r
-                                       Log.ExceptionLog(e);\r
-                               }\r
-                       }\r
-\r
-                       try {\r
-                               if (zipInStream != null) {\r
-                                       zipInStream.closeEntry();\r
-                               }\r
-                       } catch (IOException e) {\r
-                               if (zipEntry != null) {\r
-                                       Log.err("Fail to close output stream : "\r
-                                               + zipEntry.getName());\r
-                               } else {\r
-                                       Log.ExceptionLog(e);\r
-                               }\r
-                       }\r
-                       \r
-                       if (zipInStream != null) {\r
-                               try {\r
-                                       zipInStream.close();\r
-                               } catch (IOException e) {\r
-                                       Log.ExceptionLog(e);\r
-                               }\r
-                       }\r
-\r
-                       try {\r
-                               if (mFileOutput != null) {\r
-                                       mFileOutput.close();\r
-                               }\r
-                       } catch (IOException e) {\r
-                               if (zipEntry != null) {\r
-                                       Log.err("Fail to close output stream : "\r
-                                               + zipEntry.getName());\r
-                               } else {\r
-                                       Log.ExceptionLog(e);\r
-                               }\r
-                       }\r
-\r
-                       closeFileOutput();\r
-               }\r
-       }\r
-    \r
-    private void writeFileList(String filePath) {\r
-       if (filePath == null || filePath.equals("")) {\r
-               return;\r
-       }\r
-       \r
-       filePath = removeDataDirectory(filePath);\r
-       if (filePath == null || filePath.equals("")) {\r
-               return;\r
-       }\r
-       \r
-       if (mFileOutput == null) {\r
-               return;\r
-       }\r
-       \r
-       try {\r
-                       mFileOutput.write((filePath + "\n").getBytes());\r
-               } catch (IOException e) {\r
-                       Log.ExceptionLog(e);\r
-                       Log.err("Fail to unpack " + filePath);\r
-            throw new IMFatalException(ErrorCode.CANNOT_UNPACK_PACKAGE);\r
-               }\r
-    }\r
-    \r
-    private String removeDataDirectory(String filePath) {\r
-       if (filePath == null || filePath.equals("")) {\r
-               return "";\r
-       }\r
-       \r
-       if (!filePath.startsWith(DATA_DIRECTORY)) {\r
-               return "";\r
-       }\r
-       \r
-       filePath = filePath.substring(DATA_DIRECTORY.length());\r
-       return filePath;\r
-    }\r
-}\r
+/*
+*  InstallManager
+*
+* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+*
+* Contact: 
+* Wooyoung Cho <wooyoung1.cho@samsung.com>
+* Shihyun Kim <shihyun.kim@samsung.com>
+* Taeyoung Son <taeyoung2.son@samsung.com>
+* Yongsung kim <yongsung1.kim@samsung.com>
+* 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+* http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*
+* Contributors:
+* - S-Core Co., Ltd
+*
+*/ 
+
+package org.tizen.installmanager.lib.win;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+import org.tizen.installmanager.core.IMFatalException;
+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.util.unzip.ApacheSdkUnzip;
+import org.tizen.installmanager.util.unzip.UnzipSDK;
+
+
+/**
+ * Manages to unpack package of tizen sdk for windows.
+ * @author Shihyun Kim <shihyun.kim@samsung.com>
+ *
+ */
+public class WindowsSDKPackageFormat extends SDKPackageFormat{
+
+    final static int BUF_SIZE = 65536;
+    
+    private static final String DATA_DIRECTORY = "data/";
+    
+    @Override 
+    public long unZip(File fileFullPath, File targetDir, IIMProgressMonitor monitor) {
+        Log.log("Unzip "+fileFullPath+" to "+targetDir);
+        
+        PackageType checkExtension = getPackageType(fileFullPath.getAbsolutePath());
+        
+        if (!fileFullPath.exists()) {
+               Log.err("Package file is not exsisting");
+               return ERROR;
+        } else if (checkExtension != PackageType.ZIP) {
+               Log.err("Package extension is not '.zip'");
+               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 startTime = System.currentTimeMillis();
+//             long ret = unZipPackage(fileFullPath, targetDir, monitor);
+//             long finishedTime = System.currentTimeMillis();
+//             System.out.println("extracting time : "
+//                             + (finishedTime - startTime));
+               UnzipSDK unzipSdk = new ApacheSdkUnzip();
+               long ret = unzipSdk.unzipPackageFile(fileFullPath.getAbsolutePath(), targetDir.getAbsolutePath(),monitor);      
+        
+        //closeFileOutput(); 
+        return ret;
+    }
+    
+       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) {
+                               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);
+                                               //System.out.println("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);
+                                       
+                                       if (monitor != null) {
+                                               monitor.workedInstallSize(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);
+                               }
+                       }
+
+                       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 String removeDataDirectory(String filePath) {
+       if (filePath == null || filePath.equals("")) {
+               return "";
+       }
+       
+       if (!filePath.startsWith(DATA_DIRECTORY)) {
+               return "";
+       }
+       
+       filePath = filePath.substring(DATA_DIRECTORY.length());
+       return filePath;
+    }
+}