How to zipped a file or directory in java

There are times when we have to zipped a file or directory recursively in Java. This is how we do it: public static void zipFile(File file) ...

There are times when we have to zipped a file or directory recursively in Java. This is how we do it:

public static void zipFile(File file) throws IOException {
byte[] buffer = new byte[1024];

FileOutputStream fos = new FileOutputStream(
file.getParent() + File.separator + FilenameUtils.removeExtension(file.getName()) + ".zip");
ZipOutputStream zos = new ZipOutputStream(fos);
ZipEntry ze = new ZipEntry(file.getName());
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(file);

int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}

in.close();
zos.closeEntry();

// remember close it
zos.close();
}

And now this is how we zipped a folder recursively:

public static void zipDir(File file) throws IOException {
FileOutputStream fos = new FileOutputStream(new File(FilenameUtils.removeExtension(file.getParent() + File.separator + file.getName()) + ".zip"));
ZipOutputStream zos = new ZipOutputStream(fos);
FileUtils.addDirToArchive(getRootDir(), file.getPath(), zos);
fos.flush();
zos.close();
fos.close();
}

public static void addDirToArchive(String relativeRoot, String dir2zip, ZipOutputStream zos) throws IOException {
File zipDir = new File(dir2zip);
String[] dirList = zipDir.list();
byte[] readBuffer = new byte[2156];
int bytesIn = 0;

for (int i = 0; i < dirList.length; i++) {
File f = new File(zipDir, dirList[i]);
if (f.isDirectory()) {
String filePath = f.getPath();
addDirToArchive(relativeRoot, filePath, zos);
continue;
}

FileInputStream fis = new FileInputStream(f);
String relativePath = Paths.get(relativeRoot).relativize(f.toPath()).toString();
ZipEntry anEntry = new ZipEntry(relativePath);
zos.putNextEntry(anEntry);

while ((bytesIn = fis.read(readBuffer)) != -1) {
zos.write(readBuffer, 0, bytesIn);
}

fis.close();
}
}

*FilenameUtils is from apache commons

COMMENTS

mas template
Name

amazon,1,angular,8,bigdata,2,business,1,course-spring,27,courses,6,database,4,docker,3,java,50,kafka,1,keycloak,4,microservices,5,mysql,1,neworking,1,nosql,2,php,1,pinned,2,react,3,server management,7,shared drive,1,spring,7,synology,1,troubleshooting,2,web,1,wordpress,1,
ltr
item
toztech: How to zipped a file or directory in java
How to zipped a file or directory in java
toztech
https://toztech.blogspot.com/2017/09/how-to-zipped-file-or-directory-in-java.html
https://toztech.blogspot.com/
https://toztech.blogspot.com/
https://toztech.blogspot.com/2017/09/how-to-zipped-file-or-directory-in-java.html
true
2554149350007112447
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content