@@ -9,68 +9,69 @@ import java.util.zip.ZipInputStream
99 * a destination directory.
1010 * @author www.codejava.net
1111 */
12+ @Suppress(" unused" )
1213class UnzipUtility {
13- /* *
14- * Extracts a zip file specified by the zipFilePath to a directory specified by
15- * destDirectory (will be created if does not exists)
16- * @param zipFilePath
17- * @param destDirectory
18- * @throws IOException
19- */
20- @Throws(IOException ::class )
21- fun unzip (zipStream : InputStream , destDirectory : String ) {
22- val destDir = File (destDirectory)
23- if (! destDir.exists()) {
24- destDir.mkdirs()
25- }
26- val zipIn = ZipInputStream (zipStream)
27- var entry = zipIn.nextEntry
28- // iterates over entries in the zip file
29- while (entry != null ) {
30- val filePath = destDirectory + File .separator + entry.name
31- if (! entry.isDirectory) { // if the entry is a file, extracts it
32- extractFile(zipIn, filePath)
33- } else { // if the entry is a directory, make the directory
34- val dir = File (filePath)
35- dir.mkdir()
14+ companion object {
15+ /* *
16+ * Size of the buffer to read/write data
17+ */
18+ private const val BUFFER_SIZE = 4096
19+
20+ /* *
21+ * Extracts a zip file from the zipStream to a directory specified by
22+ * destDirectory (will be created if does not exists)
23+ * @param zipStream
24+ * @param destDirectory
25+ * @throws IOException
26+ */
27+ @Throws(IOException ::class )
28+ fun unzip (zipStream : InputStream , destDirectory : String ) {
29+ val destDir = File (destDirectory)
30+ if (! destDir.exists()) {
31+ destDir.mkdirs()
3632 }
37- zipIn.closeEntry()
38- entry = zipIn.nextEntry
33+ val zipIn = ZipInputStream (zipStream)
34+ var entry = zipIn.nextEntry
35+ // iterates over entries in the zip file
36+ while (entry != null ) {
37+ val filePath = destDirectory + File .separator + entry.name
38+ if (! entry.isDirectory) { // if the entry is a file, extracts it
39+ extractFile(zipIn, filePath)
40+ } else { // if the entry is a directory, make the directory
41+ val dir = File (filePath)
42+ dir.mkdir()
43+ }
44+ zipIn.closeEntry()
45+ entry = zipIn.nextEntry
46+ }
47+ zipIn.close()
3948 }
40- zipIn.close()
41- }
4249
43- /* *
44- * Extracts a zip file specified by the zipFilePath to a directory specified by
45- * destDirectory (will be created if does not exists)
46- * @param zipFilePath
47- * @param destDirectory
48- * @throws IOException
49- */
50- @Throws(IOException ::class )
51- fun unzip (zipFilePath : String , destDirectory : String ) = unzip(FileInputStream (zipFilePath), destDirectory)
52-
53- /* *
54- * Extracts a zip entry (file entry)
55- * @param zipIn
56- * @param filePath
57- * @throws IOException
58- */
59- @Throws(IOException ::class )
60- private fun extractFile (zipIn : ZipInputStream , filePath : String ) {
61- val bos = BufferedOutputStream (FileOutputStream (filePath))
62- val bytesIn = ByteArray (BUFFER_SIZE )
63- var read: Int
64- while (zipIn.read(bytesIn).also { read = it } != - 1 ) {
65- bos.write(bytesIn, 0 , read)
66- }
67- bos.close()
68- }
50+ /* *
51+ * Extracts a zip file specified by the zipFilePath to a directory specified by
52+ * destDirectory (will be created if does not exists)
53+ * @param zipFilePath
54+ * @param destDirectory
55+ * @throws IOException
56+ */
57+ @Throws(IOException ::class )
58+ fun unzip (zipFilePath : String , destDirectory : String ) = unzip(FileInputStream (zipFilePath), destDirectory)
6959
70- companion object {
7160 /* *
72- * Size of the buffer to read/write data
61+ * Extracts a zip entry (file entry)
62+ * @param zipIn
63+ * @param filePath
64+ * @throws IOException
7365 */
74- private const val BUFFER_SIZE = 4096
66+ @Throws(IOException ::class )
67+ private fun extractFile (zipIn : ZipInputStream , filePath : String ) {
68+ val bos = BufferedOutputStream (FileOutputStream (filePath))
69+ val bytesIn = ByteArray (BUFFER_SIZE )
70+ var read: Int
71+ while (zipIn.read(bytesIn).also { read = it } != - 1 ) {
72+ bos.write(bytesIn, 0 , read)
73+ }
74+ bos.close()
75+ }
7576 }
7677}
0 commit comments