Java 如何将存档文件的指定目录放入所需位置?
我想为存档文件创建一个指定的目录,并根据我想要的位置进行存储? 可以创建吗?Java 如何将存档文件的指定目录放入所需位置?,java,Java,我想为存档文件创建一个指定的目录,并根据我想要的位置进行存储? 可以创建吗? 如果有,请分享这些信息 我的代码: File input = new File("sample.tar.gz"); TFile sourceFile = new TFile(input); TFile targetFile = new TFile(File.createTempFile("sample", ".zip")); try { TFile.cp_rp(sourceFile, targetFile, TAr
如果有,请分享这些信息 我的代码:
File input = new File("sample.tar.gz");
TFile sourceFile = new TFile(input);
TFile targetFile = new TFile(File.createTempFile("sample", ".zip"));
try
{
TFile.cp_rp(sourceFile, targetFile, TArchiveDetector.NULL);
}
finally
{
TFile.umount(targetFile);
}
请尝试以下操作:
import java.io.*;
import java.util.zip.*;
/**
* This class defines two static methods for gzipping files and zipping
* directories. It also defines a demonstration program as a nested class.
**/
public class Compress {
/** Gzip the contents of the from file and save in the to file. */
public static void gzipFile(String from, String to) throws IOException {
// Create stream to read from the from file
FileInputStream in = new FileInputStream(from);
// Create stream to compress data and write it to the to file.
GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
// Copy bytes from one stream to the other
byte[] buffer = new byte[4096];
int bytes_read;
while((bytes_read = in.read(buffer)) != -1)
out.write(buffer, 0, bytes_read);
// And close the streams
in.close();
out.close();
}
/** Zip the contents of the directory, and save it in the zipfile */
public static void zipDirectory(String dir, String zipfile)
throws IOException, IllegalArgumentException {
// Check that the directory is a directory, and get its contents
File d = new File(dir);
if (!d.isDirectory())
throw new IllegalArgumentException("Compress: not a directory: " + dir);
String[] entries = d.list();
byte[] buffer = new byte[4096]; // Create a buffer for copying
int bytes_read;
// Create a stream to compress data and write it to the zipfile
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
// Loop through all entries in the directory
for(int i = 0; i < entries.length; i++) {
File f = new File(d, entries[i]);
if (f.isDirectory()) continue; // Don't zip sub-directories
FileInputStream in = new FileInputStream(f); // Stream to read file
ZipEntry entry = new ZipEntry(f.getPath()); // Make a ZipEntry
out.putNextEntry(entry); // Store entry in zipfile
while((bytes_read = in.read(buffer)) != -1) // Copy bytes to zipfile
out.write(buffer, 0, bytes_read);
in.close(); // Close input stream
}
// When we're done with the whole loop, close the output stream
out.close();
}
/**
* This nested class is a test program that demonstrates the use of the
* static methods defined above.
**/
public static class Test {
/**
* Compress a specified file or directory. If no destination name is
* specified, append .gz to a file name or .zip to a directory name
**/
public static void main(String args[]) throws IOException {
if ((args.length != 1) && (args.length != 2)) { // check arguments
System.err.println("Usage: java Compress$Test <from> [<to>]");
System.exit(0);
}
String from = args[0], to;
File f = new File(from);
boolean directory = f.isDirectory(); // Is it a file or directory?
if (args.length == 2) to = args[1];
else { // If destination not specified
if (directory) to = from + ".zip"; // use a .zip suffix
else to = from + ".gz"; // or a .gz suffix
}
if ((new File(to)).exists()) { // Make sure not to overwrite anything
System.err.println("Compress: won't overwrite existing file: " + to);
System.exit(0);
}
// Finally, call one of the methods defined above to do the work.
if (directory) Compress.zipDirectory(from, to);
else Compress.gzipFile(from, to);
}
}
}
import java.io.*;
导入java.util.zip.*;
/**
*这个类定义了两个用于压缩文件和压缩的静态方法
*目录。它还将演示程序定义为嵌套类。
**/
公共类压缩{
/**Gzip从文件中删除内容并保存到to文件中*/
公共静态void gzip文件(字符串从,字符串到)引发IOException{
//创建要从from文件读取的流
FileInputStream in=新的FileInputStream(来自);
//创建流以压缩数据并将其写入to文件。
GZIPOutputStream out=newgzipoutputstream(newfileoutputstream(to));
//将字节从一个流复制到另一个流
字节[]缓冲区=新字节[4096];
读取整数字节;
而((字节读取=in.read(缓冲区))!=-1)
写入(缓冲区,0,字节\读取);
//关闭溪流
in.close();
out.close();
}
/**压缩目录的内容,并将其保存在zipfile中*/
公共静态void zipDirectory(字符串目录、字符串zipfile)
抛出IOException、IllegalArgumentException{
//检查目录是否为目录,并获取其内容
文件d=新文件(目录);
如果(!d.isDirectory())
抛出新的IllegalArgumentException(“压缩:不是目录:“+dir”);
String[]entries=d.list();
byte[]buffer=新字节[4096];//创建用于复制的缓冲区
读取整数字节;
//创建一个流来压缩数据并将其写入zipfile
ZipOutputStream out=newzipoutpstream(newfileoutputstream(zipfile));
//循环遍历目录中的所有条目
for(int i=0;i
是否可以使用java创建?是的,这是可能的。你试过什么?您需要解压缩ZIP文件,将其放入其中,然后需要重新打包