Skip to content

Commit

Permalink
#176 #189 Changes to exclude file filter and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanth-lingala committed May 20, 2020
1 parent 7bd3ff7 commit bc8e344
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 20 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ new ZipFile("filename.zip").addFiles(Arrays.asList(new File("first_file"), new F
new ZipFile("filename.zip").addFolder(new File("/user/myuser/folder_to_add"));
~~~~

Since v2.6, it is possible to exclude certain files when adding a folder to zip by using an ExcludeFileFilter

~~~~
List<File> filesToExclude = Arrays.asList(new File("sample.pdf"), new File("sample_2.txt"));
ExcludeFileFilter excludeFileFilter = filesToExclude::contains;
new ZipFile("filename.zip").addFolder(new File("/user/myuser/folder_to_add"), new ZipParameters, excludeFileFilter);
~~~~

### Creating a zip file from stream / Adding a stream to an existing zip

~~~~
Expand Down
46 changes: 42 additions & 4 deletions src/main/java/net/lingala/zip4j/ZipFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.lingala.zip4j.headers.HeaderWriter;
import net.lingala.zip4j.io.inputstream.NumberedSplitRandomAccessFile;
import net.lingala.zip4j.io.inputstream.ZipInputStream;
import net.lingala.zip4j.model.ExcludeFileFilter;
import net.lingala.zip4j.model.FileHeader;
import net.lingala.zip4j.model.ZipModel;
import net.lingala.zip4j.model.ZipParameters;
Expand Down Expand Up @@ -181,6 +182,28 @@ public void createSplitZipFile(List<File> filesToAdd, ZipParameters parameters,
*/
public void createSplitZipFileFromFolder(File folderToAdd, ZipParameters parameters, boolean splitArchive,
long splitLength) throws ZipException {
createSplitZipFileFromFolder(folderToAdd, parameters, splitArchive, splitLength, null);
}

/**
* Creates a zip file and adds the files/folders from the specified folder to the zip file.
* This method does the same functionality as in addFolder method except that this method
* can also create split zip files when adding a folder. To create a split zip file, set the
* splitArchive parameter to true and specify the splitLength. Split length has to be more than
* or equal to 65536 bytes. Note that this method throws an exception if the zip file already
* exists.
*
* This method excludes any files defined by excludeFileFilter from adding to the zip file
*
* @param folderToAdd
* @param parameters
* @param splitArchive
* @param splitLength
* @param excludeFileFilter filter to use to exclude adding any files to the zip file
* @throws ZipException
*/
public void createSplitZipFileFromFolder(File folderToAdd, ZipParameters parameters, boolean splitArchive,
long splitLength, ExcludeFileFilter excludeFileFilter) throws ZipException {

if (folderToAdd == null) {
throw new ZipException("folderToAdd is null, cannot create zip file from folder");
Expand All @@ -202,7 +225,7 @@ public void createSplitZipFileFromFolder(File folderToAdd, ZipParameters paramet
zipModel.setSplitLength(splitLength);
}

addFolder(folderToAdd, parameters, false);
addFolder(folderToAdd, parameters, excludeFileFilter, false);
}

/**
Expand Down Expand Up @@ -329,6 +352,20 @@ public void addFolder(File folderToAdd) throws ZipException {
* @throws ZipException
*/
public void addFolder(File folderToAdd, ZipParameters zipParameters) throws ZipException {
addFolder(folderToAdd, zipParameters, null);
}

/**
* Adds the folder in the given file object to the zip file. If zip file does not exist,
* then a new zip file is created. If input folder is invalid then an exception
* is thrown. Zip parameters for the files in the folder to be added can be set in
* the input parameters
*
* @param folderToAdd
* @param zipParameters
* @throws ZipException
*/
public void addFolder(File folderToAdd, ZipParameters zipParameters, ExcludeFileFilter excludeFileFilter) throws ZipException {
if (folderToAdd == null) {
throw new ZipException("input path is null, cannot add folder to zip file");
}
Expand All @@ -349,7 +386,7 @@ public void addFolder(File folderToAdd, ZipParameters zipParameters) throws ZipE
throw new ZipException("input parameters are null, cannot add folder to zip file");
}

addFolder(folderToAdd, zipParameters, true);
addFolder(folderToAdd, zipParameters, excludeFileFilter, true);
}

/**
Expand All @@ -360,7 +397,8 @@ public void addFolder(File folderToAdd, ZipParameters zipParameters) throws ZipE
* @param checkSplitArchive
* @throws ZipException
*/
private void addFolder(File folderToAdd, ZipParameters zipParameters, boolean checkSplitArchive) throws ZipException {
private void addFolder(File folderToAdd, ZipParameters zipParameters, ExcludeFileFilter excludeFileFilter,
boolean checkSplitArchive) throws ZipException {

readZipInfo();

Expand All @@ -375,7 +413,7 @@ private void addFolder(File folderToAdd, ZipParameters zipParameters, boolean ch
}

new AddFolderToZipTask(zipModel, password, headerWriter, buildAsyncParameters()).execute(
new AddFolderToZipTaskParameters(folderToAdd, zipParameters, charset));
new AddFolderToZipTaskParameters(folderToAdd, zipParameters, excludeFileFilter, charset));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.io.File;

public interface ExcludeFileHandler {
public interface ExcludeFileFilter {

boolean isExcluded(File file);

Expand Down
10 changes: 0 additions & 10 deletions src/main/java/net/lingala/zip4j/model/ZipParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public enum SymbolicLinkAction {
private String rootFolderNameInZip;
private String fileComment;
private SymbolicLinkAction symbolicLinkAction = SymbolicLinkAction.INCLUDE_LINKED_FILE_ONLY;
private ExcludeFileHandler excludeFileHandler;

/**
* Create a ZipParameters instance with default values;
Expand Down Expand Up @@ -100,7 +99,6 @@ public ZipParameters(ZipParameters zipParameters) {
this.rootFolderNameInZip = zipParameters.getRootFolderNameInZip();
this.fileComment = zipParameters.getFileComment();
this.symbolicLinkAction = zipParameters.getSymbolicLinkAction();
this.excludeFileHandler = zipParameters.getExcludeFileHandler();
}

/**
Expand Down Expand Up @@ -382,12 +380,4 @@ public SymbolicLinkAction getSymbolicLinkAction() {
public void setSymbolicLinkAction(SymbolicLinkAction symbolicLinkAction) {
this.symbolicLinkAction = symbolicLinkAction;
}

public ExcludeFileHandler getExcludeFileHandler() {
return excludeFileHandler;
}

public void setExcludeFileHandler(ExcludeFileHandler excludeFileHandler) {
this.excludeFileHandler = excludeFileHandler;
}
}
10 changes: 7 additions & 3 deletions src/main/java/net/lingala/zip4j/tasks/AddFolderToZipTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.headers.HeaderWriter;
import net.lingala.zip4j.model.ExcludeFileFilter;
import net.lingala.zip4j.model.ZipModel;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.progress.ProgressMonitor;
Expand Down Expand Up @@ -33,7 +34,7 @@ protected long calculateTotalWork(AddFolderToZipTaskParameters taskParameters) t
List<File> filesToAdd = getFilesInDirectoryRecursive(taskParameters.folderToAdd,
taskParameters.zipParameters.isReadHiddenFiles(),
taskParameters.zipParameters.isReadHiddenFolders(),
taskParameters.zipParameters.getExcludeFileHandler());
taskParameters.excludeFileFilter);

if (taskParameters.zipParameters.isIncludeRootFolder()) {
filesToAdd.add(taskParameters.folderToAdd);
Expand Down Expand Up @@ -63,7 +64,7 @@ private List<File> getFilesToAdd(AddFolderToZipTaskParameters taskParameters) th
List<File> filesToAdd = getFilesInDirectoryRecursive(taskParameters.folderToAdd,
taskParameters.zipParameters.isReadHiddenFiles(),
taskParameters.zipParameters.isReadHiddenFolders(),
taskParameters.zipParameters.getExcludeFileHandler());
taskParameters.excludeFileFilter);

if (taskParameters.zipParameters.isIncludeRootFolder()) {
filesToAdd.add(taskParameters.folderToAdd);
Expand All @@ -75,11 +76,14 @@ private List<File> getFilesToAdd(AddFolderToZipTaskParameters taskParameters) th
public static class AddFolderToZipTaskParameters extends AbstractZipTaskParameters {
private File folderToAdd;
private ZipParameters zipParameters;
private ExcludeFileFilter excludeFileFilter;

public AddFolderToZipTaskParameters(File folderToAdd, ZipParameters zipParameters, Charset charset) {
public AddFolderToZipTaskParameters(File folderToAdd, ZipParameters zipParameters, ExcludeFileFilter excludeFileFilter,
Charset charset) {
super(charset);
this.folderToAdd = folderToAdd;
this.zipParameters = zipParameters;
this.excludeFileFilter = excludeFileFilter;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/lingala/zip4j/util/FileUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package net.lingala.zip4j.util;

import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ExcludeFileHandler;
import net.lingala.zip4j.model.ExcludeFileFilter;
import net.lingala.zip4j.model.ZipModel;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.progress.ProgressMonitor;
Expand Down Expand Up @@ -94,7 +94,7 @@ public static List<File> getFilesInDirectoryRecursive(File path, boolean readHid
return getFilesInDirectoryRecursive(path, readHiddenFiles, readHiddenFolders, null);
}

public static List<File> getFilesInDirectoryRecursive(File path, boolean readHiddenFiles, boolean readHiddenFolders, ExcludeFileHandler excludedFiles)
public static List<File> getFilesInDirectoryRecursive(File path, boolean readHiddenFiles, boolean readHiddenFolders, ExcludeFileFilter excludedFiles)
throws ZipException {

if (path == null) {
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/net/lingala/zip4j/AddFilesToZipIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -620,6 +621,22 @@ public void testAddFolderWithNotNormalizedPath() throws IOException {
ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 13);
}

@Test
public void testAddFolderWithExcludeFileFilter() throws IOException {
ZipFile zipFile = new ZipFile(generatedZipFile);
List<File> filesToExclude = Arrays.asList(
TestUtils.getTestFileFromResources("sample.pdf"),
TestUtils.getTestFileFromResources("sample_directory/favicon.ico")
);
ZipParameters zipParameters = new ZipParameters();
zipParameters.setIncludeRootFolder(false);

zipFile.addFolder(TestUtils.getTestFileFromResources(""), zipParameters, filesToExclude::contains);

ZipFileVerifier.verifyZipFileByExtractingAllFiles(generatedZipFile, outputFolder, 10);
verifyZipFileDoesNotContainFiles(generatedZipFile, Arrays.asList("sample.pdf", "sample_directory/favicon.ico"));
}

@Test
public void testAddStreamToZipThrowsExceptionWhenFileNameIsNull() throws IOException {
ZipFile zipFile = new ZipFile(generatedZipFile);
Expand Down Expand Up @@ -849,6 +866,17 @@ private void verifyZipFileContainsFiles(File generatedZipFile, List<String> file
verifyAllFilesAreOf(fileHeaders, compressionMethod, encryptionMethod, aesKeyStrength, aesVersion);
}

private void verifyZipFileDoesNotContainFiles(File generatedZipFile, List<String> fileNamesNotInZip) throws ZipException {
ZipFile zipFile = new ZipFile(generatedZipFile);
for (FileHeader fileHeader : zipFile.getFileHeaders()) {
for (String fileNameNotInZip : fileNamesNotInZip) {
assertThat(fileHeader.getFileName())
.withFailMessage("Expected file " + fileNameNotInZip + " to not be present in zip file")
.isNotEqualTo(fileNameNotInZip);
}
}
}

private void verifyFoldersInZip(List<FileHeader> fileHeaders, File generatedZipFile, char[] password)
throws IOException {
verifyFoldersInFileHeaders(fileHeaders);
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/net/lingala/zip4j/util/FileUtilsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -114,6 +116,21 @@ public void testIsSymbolicLinkReturnsTrueForALink() throws IOException {
assertThat(FileUtils.isSymbolicLink(linkFile.toFile()));
}

@Test
public void testGetFilesInDirectoryRecursiveWithExcludeFileFilter() throws IOException {
File rootFolder = TestUtils.getTestFileFromResources("");
List<File> filesToExclude = Arrays.asList(
TestUtils.getTestFileFromResources("бореиская.txt"),
TestUtils.getTestFileFromResources("sample_directory/favicon.ico")
);
List<File> allFiles = FileUtils.getFilesInDirectoryRecursive(rootFolder, true, true, filesToExclude::contains);

assertThat(allFiles).hasSize(10);
for (File file : allFiles) {
assertThat(filesToExclude).doesNotContain(file);
}
}

private void testInvalidOffsetsScenario(int start, int offset) throws IOException {
expectedException.expectMessage("invalid offsets");
expectedException.expect(ZipException.class);
Expand Down

0 comments on commit bc8e344

Please sign in to comment.