forked from joielechong/Zip4jAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add desktop Zip4j examples from zip4j website.
- Loading branch information
1 parent
fdbeeea
commit c0241b8
Showing
21 changed files
with
1,651 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
52 changes: 52 additions & 0 deletions
52
Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractAllFiles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2010 Srikanth Reddy Lingala | ||
* | ||
* 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. | ||
*/ | ||
|
||
package net.lingala.zip4j.examples.extract; | ||
|
||
import net.lingala.zip4j.core.ZipFile; | ||
import net.lingala.zip4j.exception.ZipException; | ||
|
||
/** | ||
* Demonstrates extracting all files from a zip file | ||
* | ||
* @author Srikanth Reddy Lingala | ||
* | ||
*/ | ||
public class ExtractAllFiles { | ||
|
||
public ExtractAllFiles() { | ||
|
||
try { | ||
// Initiate ZipFile object with the path/name of the zip file. | ||
ZipFile zipFile = new ZipFile("c:\\ZipTest\\ExtractAllFiles.zip"); | ||
|
||
// Extracts all files to the path specified | ||
zipFile.extractAll("c:\\ZipTest"); | ||
|
||
} catch (ZipException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* @param args | ||
*/ | ||
public static void main(String[] args) { | ||
new ExtractAllFiles(); | ||
} | ||
|
||
} |
146 changes: 146 additions & 0 deletions
146
Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractAllFilesWithInputStreams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright 2010 Srikanth Reddy Lingala | ||
* | ||
* 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. | ||
*/ | ||
|
||
package net.lingala.zip4j.examples.extract; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.List; | ||
|
||
import net.lingala.zip4j.core.ZipFile; | ||
import net.lingala.zip4j.exception.ZipException; | ||
import net.lingala.zip4j.io.ZipInputStream; | ||
import net.lingala.zip4j.model.FileHeader; | ||
import net.lingala.zip4j.unzip.UnzipUtil; | ||
|
||
/** | ||
* Example demonstrating the use of InputStreams | ||
* to extract files from the ZipFile | ||
*/ | ||
public class ExtractAllFilesWithInputStreams { | ||
|
||
private final int BUFF_SIZE = 4096; | ||
|
||
public ExtractAllFilesWithInputStreams() { | ||
|
||
ZipInputStream is = null; | ||
OutputStream os = null; | ||
|
||
try { | ||
// Initiate the ZipFile | ||
ZipFile zipFile = new ZipFile("C:\\ZipTest\\ExtractAllFilesWithInputStreams.zip"); | ||
String destinationPath = "c:\\ZipTest"; | ||
|
||
// If zip file is password protected then set the password | ||
if (zipFile.isEncrypted()) { | ||
zipFile.setPassword("password"); | ||
} | ||
|
||
//Get a list of FileHeader. FileHeader is the header information for all the | ||
//files in the ZipFile | ||
List fileHeaderList = zipFile.getFileHeaders(); | ||
|
||
// Loop through all the fileHeaders | ||
for (int i = 0; i < fileHeaderList.size(); i++) { | ||
FileHeader fileHeader = (FileHeader)fileHeaderList.get(i); | ||
if (fileHeader != null) { | ||
|
||
//Build the output file | ||
String outFilePath = destinationPath + System.getProperty("file.separator") + fileHeader.getFileName(); | ||
File outFile = new File(outFilePath); | ||
|
||
//Checks if the file is a directory | ||
if (fileHeader.isDirectory()) { | ||
//This functionality is up to your requirements | ||
//For now I create the directory | ||
outFile.mkdirs(); | ||
continue; | ||
} | ||
|
||
//Check if the directories(including parent directories) | ||
//in the output file path exists | ||
File parentDir = outFile.getParentFile(); | ||
if (!parentDir.exists()) { | ||
parentDir.mkdirs(); | ||
} | ||
|
||
//Get the InputStream from the ZipFile | ||
is = zipFile.getInputStream(fileHeader); | ||
//Initialize the output stream | ||
os = new FileOutputStream(outFile); | ||
|
||
int readLen = -1; | ||
byte[] buff = new byte[BUFF_SIZE]; | ||
|
||
//Loop until End of File and write the contents to the output stream | ||
while ((readLen = is.read(buff)) != -1) { | ||
os.write(buff, 0, readLen); | ||
} | ||
|
||
//Please have a look into this method for some important comments | ||
closeFileHandlers(is, os); | ||
|
||
//To restore File attributes (ex: last modified file time, | ||
//read only flag, etc) of the extracted file, a utility class | ||
//can be used as shown below | ||
UnzipUtil.applyFileAttributes(fileHeader, outFile); | ||
|
||
System.out.println("Done extracting: " + fileHeader.getFileName()); | ||
} else { | ||
System.err.println("fileheader is null. Shouldn't be here"); | ||
} | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} finally { | ||
try { | ||
closeFileHandlers(is, os); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
|
||
private void closeFileHandlers(ZipInputStream is, OutputStream os) throws IOException{ | ||
//Close output stream | ||
if (os != null) { | ||
os.close(); | ||
os = null; | ||
} | ||
|
||
//Closing inputstream also checks for CRC of the the just extracted file. | ||
//If CRC check has to be skipped (for ex: to cancel the unzip operation, etc) | ||
//use method is.close(boolean skipCRCCheck) and set the flag, | ||
//skipCRCCheck to false | ||
//NOTE: It is recommended to close outputStream first because Zip4j throws | ||
//an exception if CRC check fails | ||
if (is != null) { | ||
is.close(); | ||
is = null; | ||
} | ||
} | ||
|
||
/** | ||
* @param args | ||
*/ | ||
public static void main(String[] args) { | ||
new ExtractAllFilesWithInputStreams(); | ||
} | ||
|
||
} |
71 changes: 71 additions & 0 deletions
71
Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractByLoopAllFiles.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright 2010 Srikanth Reddy Lingala | ||
* | ||
* 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. | ||
*/ | ||
|
||
package net.lingala.zip4j.examples.extract; | ||
|
||
import java.util.List; | ||
|
||
import net.lingala.zip4j.core.ZipFile; | ||
import net.lingala.zip4j.exception.ZipException; | ||
import net.lingala.zip4j.model.FileHeader; | ||
|
||
/** | ||
* Demonstrates extraction of files from a zip file by looping through | ||
* all the files in the zip file | ||
* | ||
* @author Srikanth Reddy Lingala | ||
* | ||
*/ | ||
|
||
public class ExtractByLoopAllFiles { | ||
|
||
public ExtractByLoopAllFiles() { | ||
|
||
try { | ||
// Initiate ZipFile object with the path/name of the zip file. | ||
ZipFile zipFile = new ZipFile("c:\\ZipTest\\ExtractByLoopAllFiles.zip"); | ||
|
||
// Check to see if the zip file is password protected | ||
if (zipFile.isEncrypted()) { | ||
// if yes, then set the password for the zip file | ||
zipFile.setPassword("test123!"); | ||
} | ||
|
||
// Get the list of file headers from the zip file | ||
List fileHeaderList = zipFile.getFileHeaders(); | ||
|
||
// Loop through the file headers | ||
for (int i = 0; i < fileHeaderList.size(); i++) { | ||
FileHeader fileHeader = (FileHeader)fileHeaderList.get(i); | ||
// Extract the file to the specified destination | ||
zipFile.extractFile(fileHeader, "c:\\ZipTest\\"); | ||
} | ||
|
||
} catch (ZipException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* @param args | ||
*/ | ||
public static void main(String[] args) { | ||
new ExtractByLoopAllFiles(); | ||
|
||
} | ||
|
||
} |
106 changes: 106 additions & 0 deletions
106
Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractSelectFilesWithInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package net.lingala.zip4j.examples.extract; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
import net.lingala.zip4j.core.ZipFile; | ||
import net.lingala.zip4j.exception.ZipException; | ||
import net.lingala.zip4j.io.ZipInputStream; | ||
import net.lingala.zip4j.model.FileHeader; | ||
import net.lingala.zip4j.unzip.UnzipUtil; | ||
|
||
public class ExtractSelectFilesWithInputStream { | ||
|
||
private final int BUFF_SIZE = 4096; | ||
|
||
public ExtractSelectFilesWithInputStream() { | ||
|
||
ZipInputStream is = null; | ||
OutputStream os = null; | ||
|
||
try { | ||
// Initiate the ZipFile | ||
ZipFile zipFile = new ZipFile("C:\\ZipTest\\ExtractAllFilesWithInputStreams.zip"); | ||
String destinationPath = "c:\\ZipTest"; | ||
|
||
// If zip file is password protected then set the password | ||
if (zipFile.isEncrypted()) { | ||
zipFile.setPassword("password"); | ||
} | ||
|
||
//Get the FileHeader of the File you want to extract from the | ||
//zip file. Input for the below method is the name of the file | ||
//For example: 123.txt or abc/123.txt if the file 123.txt | ||
//is inside the directory abc | ||
FileHeader fileHeader = zipFile.getFileHeader("yourfilename"); | ||
|
||
if (fileHeader != null) { | ||
|
||
//Build the output file | ||
String outFilePath = destinationPath + System.getProperty("file.separator") + fileHeader.getFileName(); | ||
File outFile = new File(outFilePath); | ||
|
||
//Checks if the file is a directory | ||
if (fileHeader.isDirectory()) { | ||
//This functionality is up to your requirements | ||
//For now I create the directory | ||
outFile.mkdirs(); | ||
return; | ||
} | ||
|
||
//Check if the directories(including parent directories) | ||
//in the output file path exists | ||
File parentDir = outFile.getParentFile(); | ||
if (!parentDir.exists()) { | ||
parentDir.mkdirs(); //If not create those directories | ||
} | ||
|
||
//Get the InputStream from the ZipFile | ||
is = zipFile.getInputStream(fileHeader); | ||
//Initialize the output stream | ||
os = new FileOutputStream(outFile); | ||
|
||
int readLen = -1; | ||
byte[] buff = new byte[BUFF_SIZE]; | ||
|
||
//Loop until End of File and write the contents to the output stream | ||
while ((readLen = is.read(buff)) != -1) { | ||
os.write(buff, 0, readLen); | ||
} | ||
|
||
//Closing inputstream also checks for CRC of the the just extracted file. | ||
//If CRC check has to be skipped (for ex: to cancel the unzip operation, etc) | ||
//use method is.close(boolean skipCRCCheck) and set the flag, | ||
//skipCRCCheck to false | ||
//NOTE: It is recommended to close outputStream first because Zip4j throws | ||
//an exception if CRC check fails | ||
is.close(); | ||
|
||
//Close output stream | ||
os.close(); | ||
|
||
//To restore File attributes (ex: last modified file time, | ||
//read only flag, etc) of the extracted file, a utility class | ||
//can be used as shown below | ||
UnzipUtil.applyFileAttributes(fileHeader, outFile); | ||
|
||
System.out.println("Done extracting: " + fileHeader.getFileName()); | ||
} else { | ||
System.err.println("FileHeader does not exist"); | ||
} | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
/** | ||
* @param args | ||
*/ | ||
public static void main(String[] args) { | ||
new ExtractSelectFilesWithInputStream(); | ||
} | ||
|
||
} |
Oops, something went wrong.