Hi Guys I am using apache Commons-compress 1.9 to compress and decompress tar.gz file. Now the issue I am facing while decompressing the file. While decompressing it is saying the all the files inside the compressed folder not found. following is the decompressing code
public static boolean uncompressTarGz(String tarFileName, String outputDir, Logger log) throws IOException {
try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(tarFileName)))) {
TarArchiveEntry tarEntry = (TarArchiveEntry) tarArchiveInputStream.getNextEntry();
log.info("OutputDir : " + outputDir);
log.info("Tar File Name : " + tarFileName);
Files.createDirectories(Paths.get(outputDir));
while (tarEntry != null) {
log.info("Tarentry : " + tarEntry.getName());
File destPath = new File(outputDir, tarEntry.getName());
log.info("1");
if (!tarEntry.isDirectory()) {
destPath.createNewFile();
log.info("2");
try (FileOutputStream fout = new FileOutputStream(destPath);) {
IOUtils.copy(tarArchiveInputStream, fout);
} catch (Throwable e) {
log.info("Error : " + e + " reason : " + e.getMessage());
}
log.info("3");
// byte[] buffer = new byte[8192];
// int n = 0;
// while (-1 != (n = tarArchiveInputStream.read(buffer))) {
// fout.write(buffer, 0, n);
// }
} else {
destPath.mkdir();
}
tarEntry = (TarArchiveEntry) tarArchiveInputStream.getNextEntry();
}
return true;
} catch (Throwable e) {
log.info("Exception while untar : " + e.getMessage());
return false;
}
}
I getting error as /tmp/customTAR/customfiles/filesSucceeded (No such file or directory).
Can anyone please help me figure out what I might be doing wrong.
Thanks in advance.
Related
below is my method for unzipping folder to destination folder and method definition
unzip(filepath, unzipLocation);
here is my method to unzip a zip file this method works file but getting problem when my zip file comes like a.zip which have folder 2 folder (i.e abc1,abc2) and again folder abc1 have folder and it have files please help me
private void unzip(String src, String dest) {
String _location = "";
final int BUFFER_SIZE = 4096;
_location = dest;
System.out.println("_location ::: " + dest + "");
System.out.println("src ::: " + src + "");
BufferedOutputStream bufferedOutputStream = null;
FileInputStream fileInputStream;
try {
fileInputStream = new FileInputStream(src);
ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(fileInputStream));
ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
String zipEntryName = zipEntry.getName();
String name = dest.substring(dest.lastIndexOf("/") - 1);
// System.out.println("NAME "+name);
// File FileName = new File(FolderName);
File FileName = new File(_location.toString());
if (!FileName.isDirectory()) {
try {
if (FileName.mkdir()) {
} else {
}
} catch (Exception e) {
e.printStackTrace();
}
}
String loc = "";
String fname = "";
String LOC = "";
LOC = _location.toString();
// File file = new File(FolderName+"/" +zipEntryName);
System.out.println("ZIP " + zipEntryName + "");
// zipEntryName=zipEntryName.r
if (zipEntryName.contains("/")) {
String[] file = zipEntryName.split("/");
System.out.println("CHECK ::: " + file.length);
System.out.println("CHECK ::: " + file[0] + "");
String test = zipEntryName.substring(zipEntryName.lastIndexOf("/"), zipEntryName.length());
System.out.println("TEST " + test + "");
if (test.length() > 1) {
LOC = "";
// zipEntryName = file[1];
zipEntryName = file[file.length - 1];
System.out.println("ZIP UPDATED " + zipEntryName + "");
// _location=_location+"/"+file[0]+"/";
String l = "";
for (int i = 0; i < file.length - 1; i++) {
l = l + "/" + file[i];
}
// System.out.println("TESTTTTTTTT " + l + "");
// loc = _location + "/" + file[0];
// loc = _location + "/" + l;
LOC = _location + "/" + l;
File thumb = new File(LOC);
if (!thumb.exists()) {
thumb.mkdir();
}
System.out.println("createddd dir ");
System.out.println("createddd dir loc " + loc);
} else {
System.out.println("create dir ");
System.out.println("HERE _location111: : : : " + _location);
System.out.println("HERE zipEntryName1111 : : : : /" + zipEntryName);
// File thumb = new File(_location+"/"+zipEntryName);
File thumb = new File(LOC + "/" + zipEntryName);
if (!thumb.exists()) {
thumb.mkdir();
}
}
}
System.out.println("HERE _location: : : : " + _location);
System.out.println("HERE zipEntryName : : : : /" + zipEntryName);
System.out.println("HERE loc : : : : /" + loc);
// File file = new File(_location + "/" + zipEntryName);
File file = new File(LOC + "/" + zipEntryName);
if (file.exists()) {
} else {
if (zipEntry.isDirectory()) {
file.mkdirs();
} else {
byte buffer[] = new byte[BUFFER_SIZE];
FileOutputStream fileOutputStream = new FileOutputStream(file);
bufferedOutputStream = new BufferedOutputStream(fileOutputStream, BUFFER_SIZE);
int count;
while ((count = zipInputStream.read(buffer, 0, BUFFER_SIZE)) != -1) {
bufferedOutputStream.write(buffer, 0, count);
}
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
}
}
zipInputStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void unZipIt(String zipFile, String outputFolder) {
byte[] buffer = new byte[1024];
//mm System.out.println("ZIP FILE "+zipFile+"");
//mm System.out.println("outputFolder FILE "+outputFolder+"");
try {
//create output directory is not exists
File folder = new File(outputFolder);
if (!folder.exists()) {
folder.mkdir();
}
//get the zip file content
ZipInputStream zis =
new ZipInputStream(new FileInputStream(zipFile));
//get the zipped file list entry
ZipEntry ze = zis.getNextEntry();
while (ze != null) {
String fileName = ze.getName();
File newFile = new File(outputFolder + File.separator + fileName);
//mm System.out.println("file unzip : " + newFile.getAbsoluteFile());
//create all non exists folders
//else you will hit FileNotFoundException for compressed folder
new File(newFile.getParent()).mkdirs();
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
ze = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
System.out.println("Done");
} catch (IOException ex) {
ex.printStackTrace();
}
}
I am building a library for android, and it requires me to unzip files. It works on every single other file except for one file in one particular archive. I get a file not found exception on this. I am not a java expert, or an android expert, but my team is also stumped. Just hoping someone can spot something in my code that could be creating a bug.
public static void unzip(File zipFile,
String unzipFilePath,
FetchInterface responseHandler,
JSONObject json) {
final int BUFFER_SIZE = 4096;
String filename;
InputStream inputStream;
ZipInputStream zipInputStream;
ZipEntry zipEntry = null;
String path = unzipFilePath + File.separator;
try {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
String zipRootDirectory = null;
inputStream = new FileInputStream(zipFile);
zipInputStream = new ZipInputStream(new BufferedInputStream(inputStream));
byte[] buffer = new byte[BUFFER_SIZE];
int count;
// Log.d(tag, zipFile.getName());
while ((zipEntry = zipInputStream.getNextEntry()) != null) {
filename = zipEntry.getName();
Log.d(tag, "ZIP ENTRY: " + zipEntry.getName());
if (zipRootDirectory == null) {
zipRootDirectory = zipEntry.getName().split("\\/")[0];
// Log.d(tag, "ROOT DIR: " + zipRootDirectory);
}
if (zipEntry.isDirectory()) {
Log.d(tag, "ZIPENTRY IS DIR: " + zipEntry.toString());
File fmd = new File(path + filename);
fmd.mkdirs();
continue;
}
try {
FileOutputStream fout = new FileOutputStream(path + filename);
Log.e(tag, "FILENAME: " + filename);
while ((count = zipInputStream.read(buffer)) != -1) {
fout.write(buffer, 0, count);
}
fout.close();
} catch(FileNotFoundException e) {
Log.e(tag, "ERROR AT THIS FILE: " + filename);
e.printStackTrace();
}
zipInputStream.closeEntry();
}
zipInputStream.close();
File zipRootDirectoryFile = new File(path + File.separator + zipRootDirectory);
if (zipRootDirectoryFile.exists()) {
Log.d(tag, zipRootDirectoryFile.getName());
File renameFile = new File(path + File.separator + "html" + File.separator);
zipRootDirectoryFile.renameTo(renameFile);
} else {
Log.e(tag, "directory cannot be renamed as it does not exist");
}
} catch (IOException e) {
e.getStackTrace();
}
}
I am saving a file to disk after downloading it from server, but I believe it gets corrupted while saving on the disc. If the same file is downloaded using chrome on mac or using any other method, the file downloads and reads normally. The corruption seems to be in the saving process of the file. I am adding the code to help find out the problem. The file is a css file.
Corruption:
Some whitespace sort of characters appear when reading the file. A surprising thing that I tried and noticed is that if I reduce the BUFFER_SIZE to 32 from 4096, the file does not get corrupt, I couldn't figure out why. Also, reducing BUFFER_SIZE reduces whitespaces / corrupted characters.
Appreciate any pointers in the right direction. Thanks
private static final int BUFFER_SIZE = 4096;
// saves file to disk and returns the contents of the file.
public static String downloadFile(Context context, String filePath, String destParent) {
String content = null;
StringBuilder sb = new StringBuilder();
HttpURLConnection connection = null;
InputStream is = null;
FileOutputStream os = null;
String sUrl = Urls.makeWebAssetUrl(filePath); /// consider this my file URL
String destFile = getContextBaseDir(context) + (destParent != null ? File.separator + destParent : "") + File.separator + filePath;
try {
URL url = new URL(sUrl);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
File outFile = new File(destFile);
if (!outFile.getParentFile().exists()) {
if (!outFile.getParentFile().mkdirs()) {
throw new RuntimeException("Unable to create parent directories for " + filePath);
}
}
is = connection.getInputStream();
os = new FileOutputStream(outFile);
int bytesRead = 0;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = is.read(buffer)) != -1) {
sb.append(new String(buffer, 0, bytesRead, DEFAULT_ENCODING));
os.write(buffer);
}
content = sb.toString();
}
else {
LogUtils.LOGW(TAG, responseCode + " while connecting to " + sUrl + ": " + connection.getResponseMessage());
}
} catch(Exception e) {
LogUtils.LOGE(TAG, "Error while downloading " + sUrl, e);
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
LogUtils.LOGE(TAG, "Error closing inputStream while downloading " + sUrl, e);
}
}
if (os != null) {
try {
os.flush();
} catch (IOException e) {
LogUtils.LOGE(TAG, "Error flushing outputStream while downloading " + sUrl, e);
}
try {
os.close();
} catch (IOException e) {
LogUtils.LOGE(TAG, "Error closing outputStream while downloading " + sUrl, e);
}
}
}
return content;
}
os.write(buffer);
The problem is here. It should be:
os.write(buffer, 0, bytesRead);
I don't know why you are also accumulating the content in a StringBuffer and returning it as a String. That won't scale, and in any cast it's redundant. Remove.
so I'm getting this error:
java.io.FileNotFoundException: C:\Users\censored\Documents\Electrocode Productions\template\resources\images (Access is denied)
when I run this code:
package com.template;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Copy {
static File folder = new File(System.getProperty("user.home") + "/Documents/Electrocode Productions/template");
public static void writeSaves() {
File imagesFile = new File(folder + "/resources/images");
if(!imagesFile.exists()) {
try {
InputStream input = (Main.class.getResourceAsStream("/resources/images"));
BufferedInputStream buffedInput = new BufferedInputStream(input);
File fileFolder = new File(folder + "/resources/images");
try {
fileFolder.mkdirs();
} catch(SecurityException e) {
Log.error(e);
}
OutputStream output = new FileOutputStream(fileFolder);
BufferedOutputStream buffedOutput = new BufferedOutputStream(output);
byte[] buffer = new byte[input.available()];
System.out.println(buffer);
int bytesRead;
while((bytesRead = buffedInput.read(buffer)) > 0 ) {
buffedOutput.write(buffer, 0, bytesRead);
}
input.close();
output.close();
} catch(IOException e) {
Log.error(e);
}
}
despite the fact that I'm pretty sure I have access to write to the file as in another section of my code I do this:
public static void dump() {
if(!folder.exists()) {
try {
folder.mkdirs();
} catch(SecurityException e) {
Log.error(e);
}
}
Path oldFilePath = Paths.get(folder + "/latest.log");
Path newFilePath = Paths.get(folder + "/" + time.getDayOfMonth() + "." + time.getMonth() + "." + time.getYear() + "_" + time.getHour() + "." + time.getMinute() + "." + time.getSecond() + ".log");
if(Config.get("keeplogs").equals("true")) {
try(BufferedWriter writer = Files.newBufferedWriter(newFilePath, StandardOpenOption.CREATE, StandardOpenOption.APPEND, StandardOpenOption.WRITE)) {
#SuppressWarnings("resource")
Scanner scanner = new Scanner(oldFilePath);
while(scanner.hasNextLine()) {
writer.write(scanner.nextLine());
writer.newLine();
}
} catch(IOException e) {
e.printStackTrace();
}
} else if(Config.get("keeplogs").equals("false")) {
} else {
Log.warning("Unknown argument " + Config.get("log") + " in config.properties. Must be true or false");
}
}
What am I doing wrong? This maybe a duplicate but it may not be as I've looked at everything I can find on here (StackOverflow) and tried it all. Nothing seems to be working.
It seems that you created C:\Users\censored\Documents\Electrocode Productions\template\resources\images directory with mkdirs call. Then you trying to open it like a file what obviously fails.
Here is the code that answered my question:
public static void writeSaves(File src, File dest) throws IOException {
if(src.isDirectory()){
//if directory not exists, create it
if(!dest.exists()){
dest.mkdirs();
}
//list all the directory contents
String files[] = src.list();
for (String file : files) {
//construct the src and dest file structure
File srcFile = new File(src, file);
File destFile = new File(dest, file);
//recursive copy
writeSaves(srcFile,destFile);
}
}else{
//if file, then copy it
//Use bytes stream to support all file types
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
}
Thank you all who tried to help!
I have this Java method to upload a file. I am trying to cater for users trying to upload a folder by compressing that folder into a zip file and upload it instead. For some reason in my case file.isDirectory() and file.isFile() are not working correctly.. even though the filename does not contain any extension, file.isFile() is returning true and isDirectory() returns false. Also directory.list() is also acting weird by returning null.
What can be the problem? Am I doing something wrong?
public File uploadFile(FileItem item, String filename, int ticket_id) throws IOException
{
FileOutputStream out = null;
InputStream fileContent = null;
File file = null;
try
{
//fullpath returns C://MyDocuments//zerafbe//Documents//apache-tomcat-7.0.29//webapps//attachments//t50\test
StringBuffer fullPath = new StringBuffer();
fullPath.append(Attachment.attachments_path);
fullPath.append("t");
fullPath.append(Integer.toString(ticket_id));
fullPath.append(File.separator);
fullPath.append(filename);
System.out.println("filename " + filename);
file = new File(fullPath.toString());
if (!file.exists())
{
// if directory does not exist, create it
file.getParentFile().mkdirs();
}
if (file.isFile())
{
// if file is not a folder
out = new FileOutputStream(file);
fileContent = item.getInputStream();
int read = 0;
final byte[] bytes = new byte[1024];
// read all the file and write it to created file
while ((read = fileContent.read(bytes)) != -1)
{
out.write(bytes, 0, read);
}
}
else if (file.isDirectory())
{
ZipFile appZip = new ZipFile(fullPath.toString());
appZip.generateFileList(file);
appZip.zipIt(filename + ".zip");
}
}
catch (FileNotFoundException e)
{
LogFile.logError("[FileUpload.uploadFile()] " + e.getMessage());
}
catch (IOException e1)
{
LogFile.logError("[FileUpload.uploadFile()] " + e1.getMessage());
}
finally
{
if (out != null)
{
out.close();
}
if (fileContent != null)
{
fileContent.close();
}
}
return file;
}
This is the ZipFile class I am using
public class ZipFile
{
List<String> fileList = null;
String source_folder = "";
public ZipFile(String source_folder)
{
fileList = new ArrayList<String>();
this.source_folder = source_folder;
}
public void zipIt(String zipFile)
{
byte[] buffer = new byte[1024];
String source = "";
try
{
try
{
source = source_folder.substring(source_folder.lastIndexOf("\\") + 1, source_folder.length());
}
catch(Exception e)
{
source = source_folder;
}
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
for (String file : this.fileList)
{
ZipEntry ze = new ZipEntry(source + File.separator + file);
zos.putNextEntry(ze);
FileInputStream in = new FileInputStream(source_folder + File.separator + file);
int len;
while ((len = in.read(buffer)) > 0)
{
zos.write(buffer, 0, len);
}
in.close();
}
zos.closeEntry();
//remember close it
zos.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}
public void generateFileList(File node)
{
// add file only
if(node.isFile())
{
fileList.add(generateZipEntry(node.toString()));
}
if(node.isDirectory())
{
String[] subNode = node.list();
if (subNode != null) {
for(String filename : subNode)
{
generateFileList(new File (node, filename));
}
}
}
}
private String generateZipEntry(String path)
{
return path.substring(source_folder.length() + 1, path.length());
}
}
file.list() is being done in the generateFileList method in ZipFile class. I know this is returning null since I tried detecting whether the file is a folder or a file by using filename.indexOf(".") instead of isDirectory() and isFile() since they were not working. But I wish I had an explanation for this.
Thanks for your help!
if (!file.exists()) {
// if directory does not exist, create it
file.mkdirs();
}
will create directory and test file.isDirectory() will return true
It could be a problem with the path?
C://MyDocuments//zerafbe//Documents//apache-tomcat-7.0.29//webapps//attachments//t50\test
You are mixing backslash with slash...
I tested your code block
ZipFile appZip = new ZipFile(file.toString());
appZip.generateFileList(file);
appZip.zipIt(filename + ".zip");
with a local folder and it's working perfectly. I think you are passing a invalid path. This may be the cause isFile or isDirectory methods are acting strangely. Try to add a validation statement at the starting of generateFileList method using File API:
if(!node.exists) {
// return some flag to signify error OR throw a suitable Exception
}
This should work.
public String compressData(String srcDir) {
String zipFile = srcDir+".zip";
try {
// create byte buffer
byte[] buffer = new byte[1024];
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
File dir = new File(srcDir);
File[] files = dir.listFiles();
for (int i = 0; i < files.length; i++) {
System.out.println("Adding file: " + files[i].getName());
FileInputStream fis = new FileInputStream(files[i]);
// begin writing a new ZIP entry, positions the stream to the start of the entry data
zos.putNextEntry(new ZipEntry(files[i].getName()));
int length;
while ((length = fis.read(buffer)) > 0) {
zos.write(buffer, 0, length);
}
zos.closeEntry();
// close the InputStream
fis.close();
}
// close the ZipOutputStream
zos.close();
}
catch (IOException ioe) {
System.out.println("Error creating zip file" + ioe);
}
return zipFile;
}