diff --git a/Zip4jExamples/lib/zip4j_1.3.2.jar b/Zip4jExamples/lib/zip4j_1.3.2.jar new file mode 100644 index 0000000..66a19e4 Binary files /dev/null and b/Zip4jExamples/lib/zip4j_1.3.2.jar differ diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractAllFiles.java b/Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractAllFiles.java new file mode 100644 index 0000000..5924612 --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractAllFiles.java @@ -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(); + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractAllFilesWithInputStreams.java b/Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractAllFilesWithInputStreams.java new file mode 100644 index 0000000..1c871bf --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractAllFilesWithInputStreams.java @@ -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(); + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractByLoopAllFiles.java b/Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractByLoopAllFiles.java new file mode 100644 index 0000000..6df1035 --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractByLoopAllFiles.java @@ -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(); + + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractSelectFilesWithInputStream.java b/Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractSelectFilesWithInputStream.java new file mode 100644 index 0000000..30e8bf6 --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractSelectFilesWithInputStream.java @@ -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(); + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractSingleFile.java b/Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractSingleFile.java new file mode 100644 index 0000000..9fb7140 --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/extract/ExtractSingleFile.java @@ -0,0 +1,64 @@ +/* +* 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 extraction of a single file from the zip file + * + * @author Srikanth Reddy Lingala + */ + +public class ExtractSingleFile { + + public ExtractSingleFile() { + + try { + // Initiate ZipFile object with the path/name of the zip file. + ZipFile zipFile = new ZipFile("c:\\ZipTest\\ExtractSingleFile.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!"); + } + + // Specify the file name which has to be extracted and the path to which + // this file has to be extracted + zipFile.extractFile("Ronan_Keating_-_In_This_Life.mp3", "c:\\ZipTest\\"); + + // Note that the file name is the relative file name in the zip file. + // For example if the zip file contains a file "mysong.mp3" in a folder + // "FolderToAdd", then extraction of this file can be done as below: + zipFile.extractFile("FolderToAdd\\myvideo.avi", "c:\\ZipTest\\"); + + } catch (ZipException e) { + e.printStackTrace(); + } + + } + + /** + * @param args + */ + public static void main(String[] args) { + new ExtractSingleFile(); + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/misc/CheckZipFileSplitArchive.java b/Zip4jExamples/src/net/lingala/zip4j/examples/misc/CheckZipFileSplitArchive.java new file mode 100644 index 0000000..e527104 --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/misc/CheckZipFileSplitArchive.java @@ -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.misc; + +import net.lingala.zip4j.core.ZipFile; +import net.lingala.zip4j.exception.ZipException; + +/** + * Example to check if the zip file is a split archive + * + * @author Srikanth Reddy Lingala + * + */ +public class CheckZipFileSplitArchive { + + public CheckZipFileSplitArchive() { + + try { + // Initiate ZipFile object with the path/name of the zip file. + ZipFile zipFile = new ZipFile("c:\\ZipTest\\CheckZipFileSplitArchive.zip"); + + // Check if the zip file is a split archive + System.out.println("Is this zip file a split archive? " + zipFile.isSplitArchive()); + } catch (ZipException e) { + e.printStackTrace(); + } + + } + + /** + * @param args + */ + public static void main(String[] args) { + new CheckZipFileSplitArchive(); + + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/misc/ListAllFilesInZipFile.java b/Zip4jExamples/src/net/lingala/zip4j/examples/misc/ListAllFilesInZipFile.java new file mode 100644 index 0000000..d60b717 --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/misc/ListAllFilesInZipFile.java @@ -0,0 +1,69 @@ +/* +* 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.misc; + +import java.util.List; + +import net.lingala.zip4j.core.ZipFile; +import net.lingala.zip4j.exception.ZipException; +import net.lingala.zip4j.model.FileHeader; + +/** + * Lists all the files in a zip file including the properties of the file + * @author Srikanth Reddy Lingala + * + */ +public class ListAllFilesInZipFile { + + public ListAllFilesInZipFile() { + + try { + // Initiate ZipFile object with the path/name of the zip file. + ZipFile zipFile = new ZipFile("c:\\ZipTest\\ListAllFilesInZipFile.zip"); + + // 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); + // FileHeader contains all the properties of the file + System.out.println("****File Details for: " + fileHeader.getFileName() + "*****"); + System.out.println("Name: " + fileHeader.getFileName()); + System.out.println("Compressed Size: " + fileHeader.getCompressedSize()); + System.out.println("Uncompressed Size: " + fileHeader.getUncompressedSize()); + System.out.println("CRC: " + fileHeader.getCrc32()); + System.out.println("************************************************************"); + + // Various other properties are available in FileHeader. Please have a look at FileHeader + // class to see all the properties + } + + } catch (ZipException e) { + e.printStackTrace(); + } + + } + + /** + * @param args + */ + public static void main(String[] args) { + new ListAllFilesInZipFile(); + } + +} diff --git a/non_android_example/ProgressInformation.java b/Zip4jExamples/src/net/lingala/zip4j/examples/misc/ProgressInformation.java similarity index 88% rename from non_android_example/ProgressInformation.java rename to Zip4jExamples/src/net/lingala/zip4j/examples/misc/ProgressInformation.java index 40e9c2e..0607e4f 100644 --- a/non_android_example/ProgressInformation.java +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/misc/ProgressInformation.java @@ -1,3 +1,21 @@ +/* + * 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.misc; + import java.io.File; import java.util.ArrayList; @@ -128,4 +146,4 @@ public static void main(String[] args) { new ProgressInformation(); } -} +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/misc/RemoveFileFromZipFile.java b/Zip4jExamples/src/net/lingala/zip4j/examples/misc/RemoveFileFromZipFile.java new file mode 100644 index 0000000..c32116f --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/misc/RemoveFileFromZipFile.java @@ -0,0 +1,67 @@ +/* +* 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.misc; + +import net.lingala.zip4j.core.ZipFile; +import net.lingala.zip4j.exception.ZipException; +import net.lingala.zip4j.model.FileHeader; + +/** + * Demonstrates how to remove a file from a zip file + * + * @author Srikanth Reddy Lingala + * + */ +public class RemoveFileFromZipFile { + + public RemoveFileFromZipFile() { + + try { + // Initiate ZipFile object with the path/name of the zip file. + ZipFile zipFile = new ZipFile("c:\\ZipTest\\AddFilesWithAESZipEncryption.zip"); + + // Note: If this zip file is a split file then this method throws an exception as + // Zip Format Specification does not allow updating split zip files + + // Please make sure that this zip file has more than one file to completely test + // this example + + // Removal of a file from a zip file can be done in two ways: + // 1. Specify the name of the relative file to the removed + zipFile.removeFile("myvideo.avi"); + + // 2. With the FileHeader + if (zipFile.getFileHeaders() != null && zipFile.getFileHeaders().size() > 0) { + zipFile.removeFile((FileHeader)zipFile.getFileHeaders().get(0)); + } else { + System.out.println("This cannot be demonstrated as zip file does not have any files left"); + } + + } catch (ZipException e) { + e.printStackTrace(); + } + + } + + /** + * @param args + */ + public static void main(String[] args) { + new RemoveFileFromZipFile(); + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFilesDeflateComp.java b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFilesDeflateComp.java new file mode 100644 index 0000000..ffbcfc3 --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFilesDeflateComp.java @@ -0,0 +1,85 @@ +/* + * 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.zip; + +import java.io.File; +import java.util.ArrayList; + +import net.lingala.zip4j.core.ZipFile; +import net.lingala.zip4j.exception.ZipException; +import net.lingala.zip4j.model.ZipParameters; +import net.lingala.zip4j.util.Zip4jConstants; + +/** + * Example showing addition of files to Zip File using deflate compression + * + * @author Srikanth Reddy Lingala + * + */ +public class AddFilesDeflateComp { + + public AddFilesDeflateComp() { + try { + // Initiate ZipFile object with the path/name of the zip file. + // Zip file may not necessarily exist. If zip file exists, then + // all these files are added to the zip file. If zip file does not + // exist, then a new zip file is created with the files mentioned + ZipFile zipFile = new ZipFile("c:\\ZipTest\\AddFilesDeflateComp.zip"); + + // Build the list of files to be added in the array list + // Objects of type File have to be added to the ArrayList + ArrayList filesToAdd = new ArrayList(); + filesToAdd.add(new File("c:\\ZipTest\\sample.txt")); + filesToAdd.add(new File("c:\\ZipTest\\myvideo.avi")); + filesToAdd.add(new File("c:\\ZipTest\\mysong.mp3")); + + // Initiate Zip Parameters which define various properties such + // as compression method, etc. More parameters are explained in other + // examples + ZipParameters parameters = new ZipParameters(); + parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression + + // Set the compression level. This value has to be in between 0 to 9 + // Several predefined compression levels are available + // DEFLATE_LEVEL_FASTEST - Lowest compression level but higher speed of compression + // DEFLATE_LEVEL_FAST - Low compression level but higher speed of compression + // DEFLATE_LEVEL_NORMAL - Optimal balance between compression level/speed + // DEFLATE_LEVEL_MAXIMUM - High compression level with a compromise of speed + // DEFLATE_LEVEL_ULTRA - Highest compression level but low speed + parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); + + // Now add files to the zip file + // Note: To add a single file, the method addFile can be used + // Note: If the zip file already exists and if this zip file is a split file + // then this method throws an exception as Zip Format Specification does not + // allow updating split zip files + zipFile.addFiles(filesToAdd, parameters); + } catch (ZipException e) { + e.printStackTrace(); + } + + + } + + /** + * @param args + */ + public static void main(String[] args) { + new AddFilesDeflateComp(); + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFilesStoreComp.java b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFilesStoreComp.java new file mode 100644 index 0000000..b12b97b --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFilesStoreComp.java @@ -0,0 +1,76 @@ +/* + * 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.zip; + +import java.io.File; +import java.util.ArrayList; + +import net.lingala.zip4j.core.ZipFile; +import net.lingala.zip4j.exception.ZipException; +import net.lingala.zip4j.model.ZipParameters; +import net.lingala.zip4j.util.Zip4jConstants; + +/** + * Class demonstrating creation of a zip file by adding files with store + * compression (No Compression) + * + * @author Srikanth Reddy Lingala + * + */ +public class AddFilesStoreComp { + + public AddFilesStoreComp() { + + try { + // Initiate ZipFile object with the path/name of the zip file + // Zip file may not necessarily exist. If zip file exists, then + // all these files are added to the zip file. If zip file does not + // exist, then a new zip file is created with the files mentioned + ZipFile zipFile = new ZipFile("c:\\ZipTest\\AddFilesStoreComp.zip"); + + // Build the list of files to be added in the array list + // Objects of type File have to be added to the ArrayList + ArrayList filesToAdd = new ArrayList(); + filesToAdd.add(new File("c:\\ZipTest\\sample.txt")); + filesToAdd.add(new File("c:\\ZipTest\\myvideo.avi")); + filesToAdd.add(new File("c:\\ZipTest\\mysong.mp3")); + + // Initiate Zip Parameters which define various properties such + // as compression method, etc. More parameters are explained in other + // examples + ZipParameters parameters = new ZipParameters(); + parameters.setCompressionMethod(Zip4jConstants.COMP_STORE); // set compression method to store compression + + // Now add files to the zip file + // Note: To add a single file, the method addFile can be used + // Note: If the zip file already exists and if this zip file is a split file + // then this method throws an exception as Zip Format Specification does not + // allow updating split zip files + zipFile.addFiles(filesToAdd, parameters); + } catch (ZipException e) { + e.printStackTrace(); + } + } + + /** + * @param args + */ + public static void main(String[] args) { + new AddFilesStoreComp(); + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFilesToFolderInZip.java b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFilesToFolderInZip.java new file mode 100644 index 0000000..0533c42 --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFilesToFolderInZip.java @@ -0,0 +1,73 @@ +/* + * 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.zip; + +import java.io.File; +import java.util.ArrayList; + +import net.lingala.zip4j.core.ZipFile; +import net.lingala.zip4j.exception.ZipException; +import net.lingala.zip4j.model.ZipParameters; +import net.lingala.zip4j.util.Zip4jConstants; + +/** + * Example demonstrates adding files to a folder in a zip file + * @author Srikanth Reddy Lingala + * + */ +public class AddFilesToFolderInZip { + + public AddFilesToFolderInZip() { + try { + ZipFile zipFile = new ZipFile("c:\\ZipTest\\AddFilesDeflateComp.zip"); + + // Build the list of files to be added in the array list + ArrayList filesToAdd = new ArrayList(); + filesToAdd.add(new File("c:\\ZipTest\\sample.txt")); + filesToAdd.add(new File("c:\\ZipTest\\myvideo.avi")); + filesToAdd.add(new File("c:\\ZipTest\\mysong.mp3")); + + // Initiate Zip Parameters + ZipParameters parameters = new ZipParameters(); + parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression + + // Set the compression level. + parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); + + // Sets the folder in the zip file to which these new files will be added. + // In this example, test2 is the folder to which these files will be added. + // Another example: if files were to be added to a directory test2/test3, then + // below statement should be parameters.setRootFolderInZip("test2/test3/"); + parameters.setRootFolderInZip("test2/"); + + // Now add files to the zip file + zipFile.addFiles(filesToAdd, parameters); + } catch (ZipException e) { + e.printStackTrace(); + } + + + } + + /** + * @param args + */ + public static void main(String[] args) { + new AddFilesToFolderInZip(); + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFilesWithAESEncryption.java b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFilesWithAESEncryption.java new file mode 100644 index 0000000..ac5efa8 --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFilesWithAESEncryption.java @@ -0,0 +1,95 @@ +/* + * 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.zip; + +import java.io.File; +import java.util.ArrayList; + +import net.lingala.zip4j.core.ZipFile; +import net.lingala.zip4j.exception.ZipException; +import net.lingala.zip4j.model.ZipParameters; +import net.lingala.zip4j.util.Zip4jConstants; + +/** + * Demonstrates adding files to zip file with AES Encryption + * + * @author Srikanth Reddy Lingala + */ +public class AddFilesWithAESEncryption { + + public AddFilesWithAESEncryption() { + + try { + // Initiate ZipFile object with the path/name of the zip file. + ZipFile zipFile = new ZipFile("c:\\ZipTest\\AddFilesWithAESZipEncryption.zip"); + + // Build the list of files to be added in the array list + // Objects of type File have to be added to the ArrayList + ArrayList filesToAdd = new ArrayList(); + filesToAdd.add(new File("c:\\ZipTest\\sample.txt")); + filesToAdd.add(new File("c:\\ZipTest\\myvideo.avi")); + filesToAdd.add(new File("c:\\ZipTest\\mysong.mp3")); + + // Initiate Zip Parameters which define various properties such + // as compression method, etc. More parameters are explained in other + // examples + ZipParameters parameters = new ZipParameters(); + parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression + + // Set the compression level. This value has to be in between 0 to 9 + // Several predefined compression levels are available + // DEFLATE_LEVEL_FASTEST - Lowest compression level but higher speed of compression + // DEFLATE_LEVEL_FAST - Low compression level but higher speed of compression + // DEFLATE_LEVEL_NORMAL - Optimal balance between compression level/speed + // DEFLATE_LEVEL_MAXIMUM - High compression level with a compromise of speed + // DEFLATE_LEVEL_ULTRA - Highest compression level but low speed + parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); + + // Set the encryption flag to true + // If this is set to false, then the rest of encryption properties are ignored + parameters.setEncryptFiles(true); + + // Set the encryption method to AES Zip Encryption + parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES); + + // Set AES Key strength. Key strengths available for AES encryption are: + // AES_STRENGTH_128 - For both encryption and decryption + // AES_STRENGTH_192 - For decryption only + // AES_STRENGTH_256 - For both encryption and decryption + // Key strength 192 cannot be used for encryption. But if a zip file already has a + // file encrypted with key strength of 192, then Zip4j can decrypt this file + parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256); + + // Set password + parameters.setPassword("test123!"); + + // Now add files to the zip file + // Note: To add a single file, the method addFile can be used + // Note: If the zip file already exists and if this zip file is a split file + // then this method throws an exception as Zip Format Specification does not + // allow updating split zip files + zipFile.addFiles(filesToAdd, parameters); + } catch (ZipException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + new AddFilesWithAESEncryption(); + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFilesWithStandardZipEncryption.java b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFilesWithStandardZipEncryption.java new file mode 100644 index 0000000..e6bcae1 --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFilesWithStandardZipEncryption.java @@ -0,0 +1,78 @@ +/* + * 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.zip; + +import java.io.File; +import java.util.ArrayList; + +import net.lingala.zip4j.core.ZipFile; +import net.lingala.zip4j.exception.ZipException; +import net.lingala.zip4j.model.ZipParameters; +import net.lingala.zip4j.util.Zip4jConstants; + +/** + * Demonstrates adding files to zip file with standard Zip Encryption + */ +public class AddFilesWithStandardZipEncryption { + + public AddFilesWithStandardZipEncryption() { + + try { + // Initiate ZipFile object with the path/name of the zip file. + ZipFile zipFile = new ZipFile("c:\\ZipTest\\AddFilesWithStandardZipEncryption.zip"); + + // Build the list of files to be added in the array list + // Objects of type File have to be added to the ArrayList + ArrayList filesToAdd = new ArrayList(); + filesToAdd.add(new File("c:\\ZipTest\\sample.txt")); + filesToAdd.add(new File("c:\\ZipTest\\myvideo.avi")); + filesToAdd.add(new File("c:\\ZipTest\\mysong.mp3")); + + // Initiate Zip Parameters which define various properties such + // as compression method, etc. + ZipParameters parameters = new ZipParameters(); + parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to store compression + + // Set the compression level + parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); + + // Set the encryption flag to true + // If this is set to false, then the rest of encryption properties are ignored + parameters.setEncryptFiles(true); + + // Set the encryption method to Standard Zip Encryption + parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); + + // Set password + parameters.setPassword("test123!"); + + // Now add files to the zip file + // Note: To add a single file, the method addFile can be used + // Note: If the zip file already exists and if this zip file is a split file + // then this method throws an exception as Zip Format Specification does not + // allow updating split zip files + zipFile.addFiles(filesToAdd, parameters); + } catch (ZipException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + new AddFilesWithStandardZipEncryption(); + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFolder.java b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFolder.java new file mode 100644 index 0000000..989c62c --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddFolder.java @@ -0,0 +1,62 @@ +/* +* 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.zip; + +import net.lingala.zip4j.core.ZipFile; +import net.lingala.zip4j.exception.ZipException; +import net.lingala.zip4j.model.ZipParameters; +import net.lingala.zip4j.util.Zip4jConstants; + +/** + * Demonstrated adding a folder to zip file + * + * @author Srikanth Reddy Lingala + */ +public class AddFolder { + + public AddFolder() { + + try { + // Initiate ZipFile object with the path/name of the zip file. + ZipFile zipFile = new ZipFile("c:\\ZipTest\\AddFolder.zip"); + + // Folder to add + String folderToAdd = "c:\\FolderToAdd"; + + // Initiate Zip Parameters which define various properties such + // as compression method, etc. + ZipParameters parameters = new ZipParameters(); + + // set compression method to store compression + parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); + + // Set the compression level + parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); + + // Add folder to the zip file + zipFile.addFolder(folderToAdd, parameters); + + } catch (ZipException e) { + e.printStackTrace(); + } + } + + public static void main(String[] args) { + new AddFolder(); + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddStreamToZip.java b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddStreamToZip.java new file mode 100644 index 0000000..3824fac --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/AddStreamToZip.java @@ -0,0 +1,95 @@ +/* + * 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.zip; + +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + +import net.lingala.zip4j.core.ZipFile; +import net.lingala.zip4j.model.ZipParameters; +import net.lingala.zip4j.util.Zip4jConstants; + +/** + * Example demonstrating creation of a zip file with content + * from an inputstream. This inputstream may not be a file existing + * on the disk. Notes: + *
+ * + */ +public class AddStreamToZip { + + public AddStreamToZip() { + + InputStream is = null; + + try { + // Initiate ZipFile object with the path/name of the zip file. + // Zip file may not necessarily exist. If zip file exists, then + // all these files are added to the zip file. If zip file does not + // exist, then a new zip file is created with the files mentioned + ZipFile zipFile = new ZipFile("c:\\ZipTest\\AddStreamToZip.zip"); + + // Initiate Zip Parameters which define various properties such + // as compression method, etc. More parameters are explained in other + // examples + ZipParameters parameters = new ZipParameters(); + parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); + + // below two parameters have to be set for adding content to a zip file + // directly from a stream + + // this would be the name of the file for this entry in the zip file + parameters.setFileNameInZip("yourfilename.txt"); + + // we set this flag to true. If this flag is true, Zip4j identifies that + // the data will not be from a file but directly from a stream + parameters.setSourceExternalStream(true); + + // For this example I use a FileInputStream but in practise this can be + // any inputstream + is = new FileInputStream("filetoadd.txt"); + + // Creates a new entry in the zip file and adds the content to the zip file + zipFile.addStream(is, parameters); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + public static void main(String[] args) { + new AddStreamToZip(); + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/zip/CreateSplitZipFile.java b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/CreateSplitZipFile.java new file mode 100644 index 0000000..d9e348a --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/CreateSplitZipFile.java @@ -0,0 +1,76 @@ +/* +* 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.zip; + +import java.io.File; +import java.util.ArrayList; + +import net.lingala.zip4j.core.ZipFile; +import net.lingala.zip4j.exception.ZipException; +import net.lingala.zip4j.model.ZipParameters; +import net.lingala.zip4j.util.Zip4jConstants; + +/** + * Demonstrated how to create a split zip file + * + * @author Srikanth Reddy Lingala + * + */ +public class CreateSplitZipFile { + + public CreateSplitZipFile() { + + try { + // Initiate ZipFile object with the path/name of the zip file. + ZipFile zipFile = new ZipFile("c:\\ZipTest\\CreateSplitZipFile.zip"); + + // Build the list of files to be added in the array list + // Objects of type File have to be added to the ArrayList + ArrayList filesToAdd = new ArrayList(); + filesToAdd.add(new File("c:\\ZipTest\\sample.txt")); + filesToAdd.add(new File("c:\\ZipTest\\myvideo.avi")); + filesToAdd.add(new File("c:\\ZipTest\\mysong.mp3")); + + // Initiate Zip Parameters which define various properties such + // as compression method, etc. + ZipParameters parameters = new ZipParameters(); + + // set compression method to store compression + parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); + + // Set the compression level. This value has to be in between 0 to 9 + parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); + + // Create a split file by setting splitArchive parameter to true + // and specifying the splitLength. SplitLenth has to be greater than + // 65536 bytes + // Please note: If the zip file already exists, then this method throws an + // exception + zipFile.createZipFile(filesToAdd, parameters, true, 10485760); + } catch (ZipException e) { + e.printStackTrace(); + } + } + + /** + * @param args + */ + public static void main(String[] args) { + new CreateSplitZipFile(); + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/zip/CreateSplitZipFileFromFolder.java b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/CreateSplitZipFileFromFolder.java new file mode 100644 index 0000000..45d5454 --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/CreateSplitZipFileFromFolder.java @@ -0,0 +1,67 @@ +/* +* 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.zip; + +import net.lingala.zip4j.core.ZipFile; +import net.lingala.zip4j.exception.ZipException; +import net.lingala.zip4j.model.ZipParameters; +import net.lingala.zip4j.util.Zip4jConstants; + +/** + * Example showing creation of split zip file and adding a folder to this zip file + * + * @author Srikanth Reddy Lingala + * + */ +public class CreateSplitZipFileFromFolder { + + public CreateSplitZipFileFromFolder() { + + try { + // Initiate ZipFile object with the path/name of the zip file. + ZipFile zipFile = new ZipFile("c:\\ZipTest\\CreateSplitZipFileFromFolder.zip"); + + // Initiate Zip Parameters which define various properties such + // as compression method, etc. + ZipParameters parameters = new ZipParameters(); + + // set compression method to store compression + parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); + + // Set the compression level. This value has to be in between 0 to 9 + parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); + + // Create a split file by setting splitArchive parameter to true + // and specifying the splitLength. SplitLenth has to be greater than + // 65536 bytes + // Please note: If the zip file already exists, then this method throws an + // exception + zipFile.createZipFileFromFolder("C:\\ZipTest", parameters, true, 10485760); + } catch (ZipException e) { + e.printStackTrace(); + } + + } + + /** + * @param args + */ + public static void main(String[] args) { + new CreateSplitZipFileFromFolder(); + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/zip/CreateZipWithOutputStreams.java b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/CreateZipWithOutputStreams.java new file mode 100644 index 0000000..a860764 --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/CreateZipWithOutputStreams.java @@ -0,0 +1,154 @@ +/* + * 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.zip; + +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 net.lingala.zip4j.io.ZipOutputStream; +import net.lingala.zip4j.model.ZipParameters; +import net.lingala.zip4j.util.Zip4jConstants; + +/** + * This example demonstrates the use of ZipOutputStreams. + * ZipOutputStreams can be used to create zip files "on the fly" + * with all compression and encryption being done in memory + */ +public class CreateZipWithOutputStreams { + + public CreateZipWithOutputStreams() { + + // Input and OutputStreams are defined outside of the try/catch block + // to use them in the finally block + ZipOutputStream outputStream = null; + InputStream inputStream = null; + + try { + // Prepare the files to be added + ArrayList filesToAdd = new ArrayList(); + filesToAdd.add(new File("c:\\ZipTest\\sample.txt")); + filesToAdd.add(new File("c:\\ZipTest\\myvideo.avi")); + filesToAdd.add(new File("c:\\ZipTest\\mysong.mp3")); + + //Initiate output stream with the path/file of the zip file + //Please note that ZipOutputStream will overwrite zip file if it already exists + outputStream = new ZipOutputStream(new FileOutputStream(new File("c:\\ZipTest\\CreateZipFileWithOutputStreams.zip"))); + + // Initiate Zip Parameters which define various properties such + // as compression method, etc. More parameters are explained in other + // examples + ZipParameters parameters = new ZipParameters(); + + //Deflate compression or store(no compression) can be set below + parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); + + // Set the compression level. This value has to be in between 0 to 9 + // Several predefined compression levels are available + // DEFLATE_LEVEL_FASTEST - Lowest compression level but higher speed of compression + // DEFLATE_LEVEL_FAST - Low compression level but higher speed of compression + // DEFLATE_LEVEL_NORMAL - Optimal balance between compression level/speed + // DEFLATE_LEVEL_MAXIMUM - High compression level with a compromise of speed + // DEFLATE_LEVEL_ULTRA - Highest compression level but low speed + parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); + + //This flag defines if the files have to be encrypted. + //If this flag is set to false, setEncryptionMethod, as described below, + //will be ignored and the files won't be encrypted + parameters.setEncryptFiles(true); + + //Zip4j supports AES or Standard Zip Encryption (also called ZipCrypto) + //If you choose to use Standard Zip Encryption, please have a look at example + //as some additional steps need to be done while using ZipOutputStreams with + //Standard Zip Encryption + parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES); + + //If AES encryption is used, this defines the key strength + parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256); + + //self descriptive + parameters.setPassword("YourPassword"); + + //Now we loop through each file and read this file with an inputstream + //and write it to the ZipOutputStream. + for (int i = 0; i < filesToAdd.size(); i++) { + File file = (File)filesToAdd.get(i); + + //This will initiate ZipOutputStream to include the file + //with the input parameters + outputStream.putNextEntry(file,parameters); + + //If this file is a directory, then no further processing is required + //and we close the entry (Please note that we do not close the outputstream yet) + if (file.isDirectory()) { + outputStream.closeEntry(); + continue; + } + + //Initialize inputstream + inputStream = new FileInputStream(file); + byte[] readBuff = new byte[4096]; + int readLen = -1; + + //Read the file content and write it to the OutputStream + while ((readLen = inputStream.read(readBuff)) != -1) { + outputStream.write(readBuff, 0, readLen); + } + + //Once the content of the file is copied, this entry to the zip file + //needs to be closed. ZipOutputStream updates necessary header information + //for this file in this step + outputStream.closeEntry(); + + inputStream.close(); + } + + //ZipOutputStream now writes zip header information to the zip file + outputStream.finish(); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (outputStream != null) { + try { + outputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + /** + * @param args + */ + public static void main(String[] args) { + new CreateZipWithOutputStreams(); + } + +} diff --git a/Zip4jExamples/src/net/lingala/zip4j/examples/zip/CreateZipWithOutputStreamsStandardEnc.java b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/CreateZipWithOutputStreamsStandardEnc.java new file mode 100644 index 0000000..9decc35 --- /dev/null +++ b/Zip4jExamples/src/net/lingala/zip4j/examples/zip/CreateZipWithOutputStreamsStandardEnc.java @@ -0,0 +1,144 @@ +/* + * 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.zip; + +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 net.lingala.zip4j.io.ZipOutputStream; +import net.lingala.zip4j.model.ZipParameters; +import net.lingala.zip4j.util.Zip4jConstants; + +public class CreateZipWithOutputStreamsStandardEnc { + + public CreateZipWithOutputStreamsStandardEnc() { + + // Input and OutputStreams are defined outside of the try/catch block + // to use them in the finally block + ZipOutputStream outputStream = null; + InputStream inputStream = null; + + try { + // Prepare the files to be added + ArrayList filesToAdd = new ArrayList(); + filesToAdd.add(new File("c:\\ZipTest\\sample.txt")); + filesToAdd.add(new File("c:\\ZipTest\\myvideo.avi")); + filesToAdd.add(new File("c:\\ZipTest\\mysong.mp3")); + + //Initiate output stream with the path/file of the zip file + //Please note that ZipOutputStream will overwrite zip file if it already exists + outputStream = new ZipOutputStream(new FileOutputStream(new File("c:\\ZipTest\\CreateZipWithOutputStreamsStandardEnc.zip"))); + + // Initiate Zip Parameters which define various properties such + // as compression method, etc. More parameters are explained in other + // examples + ZipParameters parameters = new ZipParameters(); + + //Deflate compression or store(no compression) can be set below + parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); + + // Set the compression level. This value has to be in between 0 to 9 + // Several predefined compression levels are available + // DEFLATE_LEVEL_FASTEST - Lowest compression level but higher speed of compression + // DEFLATE_LEVEL_FAST - Low compression level but higher speed of compression + // DEFLATE_LEVEL_NORMAL - Optimal balance between compression level/speed + // DEFLATE_LEVEL_MAXIMUM - High compression level with a compromise of speed + // DEFLATE_LEVEL_ULTRA - Highest compression level but low speed + parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); + + //This flag defines if the files have to be encrypted. + //If this flag is set to false, setEncryptionMethod, as described below, + //will be ignored and the files won't be encrypted + parameters.setEncryptFiles(true); + + //Set encryption method to Standard Encryption + parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); + + //self descriptive + parameters.setPassword("YourPassword"); + + //Now we loop through each file, determine the file CRC and set it + //in the zip parameters and then we read the input stream and write it + //to the outputstream + for (int i = 0; i < filesToAdd.size(); i++) { + File file = (File)filesToAdd.get(i); + + //This will initiate ZipOutputStream to include the file + //with the input parameters + outputStream.putNextEntry(file,parameters); + + //If this file is a directory, then no further processing is required + //and we close the entry (Please note that we do not close the outputstream yet) + if (file.isDirectory()) { + outputStream.closeEntry(); + continue; + } + + //Initialize inputstream + inputStream = new FileInputStream(file); + byte[] readBuff = new byte[4096]; + int readLen = -1; + + //Read the file content and write it to the OutputStream + while ((readLen = inputStream.read(readBuff)) != -1) { + outputStream.write(readBuff, 0, readLen); + } + + //Once the content of the file is copied, this entry to the zip file + //needs to be closed. ZipOutputStream updates necessary header information + //for this file in this step + outputStream.closeEntry(); + + inputStream.close(); + } + + //ZipOutputStream now writes zip header information to the zip file + outputStream.finish(); + + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (outputStream != null) { + try { + outputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + if (inputStream != null) { + try { + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } + + /** + * @param args + */ + public static void main(String[] args) { + new CreateZipWithOutputStreamsStandardEnc(); + } + +}