Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

zip file java

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipCompress {
    public static void compress(String dirPath) {
        final Path sourceDir = Paths.get(dirPath);
        String zipFileName = dirPath.concat(".zip");
        try {
            final ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(zipFileName));
            Files.walkFileTree(sourceDir, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
                    try {
                        Path targetFile = sourceDir.relativize(file);
                        outputStream.putNextEntry(new ZipEntry(targetFile.toString()));
                        byte[] bytes = Files.readAllBytes(file);
                        outputStream.write(bytes, 0, bytes.length);
                        outputStream.closeEntry();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Comment

zip file java


StringBuilder sb = new StringBuilder();
sb.append("Test String");

File f = new File("d:	est.zip");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
ZipEntry e = new ZipEntry("mytext.txt");
out.putNextEntry(e);

byte[] data = sb.toString().getBytes();
out.write(data, 0, data.length);
out.closeEntry();

out.close();

Comment

Java Zip File

StringBuilder sb = new StringBuilder();
sb.append("Test String");

File f = new File("d:	est.zip");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(f));
ZipEntry e = new ZipEntry("mytext.txt");
out.putNextEntry(e);

byte[] data = sb.toString().getBytes();
out.write(data, 0, data.length);
out.closeEntry();

out.close();
Comment

PREVIOUS NEXT
Code Example
Java :: how to initialize a set in java 
Java :: java stream distinct by atribute 
Java :: caused by: java.lang.noclassdeffounderror: org/springframework/boot/configurationprocessor/json/jsonexception 
Java :: on touch listener android 
Java :: containskey in java 
Java :: java syntax for object creation 
Java :: multiple type array java 
Java :: android zoom animation 
Java :: print list array 
Java :: how to print an array in java 
Java :: How to print in console java 
Java :: java remove space at the end of string 
Java :: android imageview set image from drawable programmatically 
Java :: java for increment by 2 
Java :: java leap year 
Java :: loop through an arraylist android studio 
Java :: java producer consumer 
Java :: date minus date java 
Java :: java median of list 
Java :: reverse int array java 
Java :: how to make a loop in java 
Java :: primitive data types in java 
Java :: if is true javascrtiptr 
Java :: find minimum in array java 
Java :: pointers in java 
Java :: string format java 
Java :: java class name to string 
Java :: java print formatted string 
Java :: java using .indexof to fin a space 
Java :: java.lang.arrayindexoutofboundsexception: index 3 out of bounds for length 3 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =