Here is my code snippet:
public class Compress {
List<String> filesinDir= new ArrayList<String>();
public static void main(String[] args){
Compress c= new Compress();
c.gzipFile();
String OUTPUT_DIR= "C:\\Users\\Surya's\\Documents\\tmp.zip";
File dir= new File(" C:\\Users\\Surya's\\Documents\\tmp ");
c.zipDirectory(dir, OUTPUT_DIR);
}
public void gzipFile(){
String OUTPUT_GZIP_FILE= " C:\\Users\\Surya's\\Documents\\file1.gz ";
String SOURCE_FILE= " C:\\Users\\Surya's\\Documents\\file1.txt ";
byte[] b= new byte[1024];
int len;
try{
FileOutputStream fos= new FileOutputStream(OUTPUT_GZIP_FILE);
GZIPOutputStream gz= new GZIPOutputStream(fos);
FileInputStream in= new FileInputStream(SOURCE_FILE);
while((len= in.read(b))!= -1){
gz.write(b, 0, len);
}
fos.close();
in.close();
gz.finish();
gz.close();
}
catch(IOException e){
e.printStackTrace();
}
}
public void zipDirectory(File dir, String OUTPUT_DIR){
try{
ListofFiles(dir);
FileOutputStream of= new FileOutputStream(OUTPUT_DIR);
ZipOutputStream gzdir= new ZipOutputStream(of);
for(String filepath : filesinDir){
ZipEntry ze= new ZipEntry(filepath.substring(dir.getAbsolutePath().length()+1, filepath.length()));
gzdir.putNextEntry(ze);
byte[] b= new byte[1024];
int len;
FileInputStream fi= new FileInputStream(filepath);
while((len=fi.read(b))!=-1){
of.write(b, 0, len);
}
gzdir.closeEntry();
fi.close();
}
gzdir.close();
of.close();
}
catch(IOException e){
e.printStackTrace();
}
}
public void ListofFiles(File dir) throws IOException{
File[] files= dir.listFiles();
for(File file : files){
if(file.isFile()) filesinDir.add(file.getAbsolutePath());
else ListofFiles(file);
}
}
}
I am trying to zip a single file as well as well as a directory with files in it. gzipFile() handles the compression of the single file while zipDirectory() calls a function ListofFiles() for arranging the abstract pathnames in an array. zipDirectory uses ZipEntry to begin writing from the start of the file and positions the start of the
The error message is
java.io.FileNotFoundException: C:\Users\Surya's\Documents\file1.gz (The filename, directory name, or volume label syntax is incorrect)
at java.io.FileOutputStream.open(Native Method)
at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
at java.io.FileOutputStream.<init>(FileOutputStream.java:110)
at com.assignment.java.Compress.gzipFile(Compress.java:29)
at com.assignment.java.Compress.main(Compress.java:15)
Exception in thread "main" java.lang.NullPointerException
at com.assignment.java.Compress.ListofFiles(Compress.java:73)
at com.assignment.java.Compress.zipDirectory(Compress.java:48)
at com.assignment.java.Compress.main(Compress.java:18)
Why is a FileNotFound exception being shown since the program is supposed to create a file file1.gz in the Documents folder.
You shouldn't put extra spaces to the path.
Try using
String OUTPUT_DIR= "C:\\Users\\Surya's\\Documents\\tmp.zip";
File dir= new File("C:\\Users\\Surya's\\Documents\\tmp");
String OUTPUT_GZIP_FILE= "C:\\Users\\Surya's\\Documents\\file1.gz";
String SOURCE_FILE= "C:\\Users\\Surya's\\Documents\\file1.txt";
instead of
String OUTPUT_DIR= "C:\\Users\\Surya's\\Documents\\tmp.zip";
File dir= new File(" C:\\Users\\Surya's\\Documents\\tmp ");
String OUTPUT_GZIP_FILE= " C:\\Users\\Surya's\\Documents\\file1.gz ";
String SOURCE_FILE= " C:\\Users\\Surya's\\Documents\\file1.txt ";
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.