Closed pyotr777 closed 11 years ago
Here is my way of storing symlinks. This function takes two arguments: list of files to put into the archive and the name of tar archive.
/** * Zip files from file_list into archive zipFile * @param zipFile output ZIP file location */ public void tarIt(ArrayListfile_list, File tar_file) throws IOException, FileNotFoundException,SecurityException { TarOutputStream tos=null; try{ FileOutputStream fos = new FileOutputStream(tar_file); tos = new TarOutputStream( new BufferedOutputStream(fos)); System.out.println("Creating tar " + tar_file + " with files:"); for(File file : file_list) { System.out.print(file); TarEntry te= new TarEntry(file, generateTarEntry(file.getAbsolutePath())); te.setModTime(file.lastModified()); Date modificationTime = te.getModTime(); System.out.print("\t\t"+modificationTime); // copy file owner permissions // default permission: 644 TarHeader tar_header = te.getHeader(); if (file.canExecute()) tar_header.mode = tar_header.mode | 0100; // add execute permission if (file.canWrite()) tar_header.mode = tar_header.mode | 0400; // add write permission else tar_header.mode = tar_header.mode & 0177577; // remove write permission System.out.println("\t\t "+ Integer.toOctalString(tar_header.mode - 0100000)+"\t"+tar_header.size); if (Files.isSymbolicLink(file.toPath())) { tar_header.linkFlag=TarHeader.LF_SYMLINK; String target_path = Files.readSymbolicLink(file.toPath()).toString(); tar_header.linkName=new StringBuffer(target_path); tar_header.size = 0; tos.putNextEntry(new TarEntry(tar_header)); } else { tos.putNextEntry(new TarEntry(tar_header)); BufferedInputStream origin = new BufferedInputStream(new FileInputStream( file )); int count; byte data[] = new byte[2048]; while((count = origin.read(data)) != -1) { tos.write(data, 0, count); } origin.close(); } tos.flush(); } } finally { tos.close(); } }
Thankyou for your contribution.
Can't find a way to store symlinks as symlinks in tar archive. Symlinks get stored as their target files.